zoukankan      html  css  js  c++  java
  • RabbitMq之RPC

    前言

    产生背景

    在前几篇文章中,客户端都是向服务端发送消息,但是在实际情况中,常常会有如需要接受端接受消息,并且进行处理之后再次返回给那个客户端

    处理方法描述

    发送端在发送信息前,产生一个接收消息的临时队列,该队列用来接收返回的结果。其实在这里接收端、发送端的概念已经比较模糊了,因为发送端也同样要接收消息,接收端同样也要发送消息,所以这里笔者使用另外的示例来演示这一过程。

    示例内容

    假设有一个控制中心和一个计算节点,控制中心会将一个自然数N发送给计算节点,计算节点将N值加1后,返回给控制中心。这里用center.py模拟控制中心,compute.py模拟计算节点。

    代码

    center

    import pika
     
    class Center(object):
        def __init__(self):
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(
                    host='localhost'))
     
            self.channel = self.connection.channel()
             
            #定义接收返回消息的队列
            result = self.channel.queue_declare(exclusive=True)
            self.callback_queue = result.method.queue
     
            self.channel.basic_consume(self.on_response,
                                       no_ack=True,
                                       queue=self.callback_queue)
     
        #定义接收到返回消息的处理方法
        def on_response(self, ch, method, props, body):
            self.response = body
         
         
        def request(self, n):
            self.response = None
            #发送计算请求,并声明返回队列
            self.channel.basic_publish(exchange='',
                                       routing_key='compute_queue',
                                       properties=pika.BasicProperties(
                                             reply_to = self.callback_queue,
                                             ),
                                       body=str(n))
            #接收返回的数据
            while self.response is None:
                self.connection.process_data_events()
            return int(self.response)
     
    center = Center()
     
    print " [x] Requesting increase(30)"
    response = center.request(30)
    print " [.] Got %r" % (response,)

    computer

    import pika
     
    #连接rabbitmq服务器
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
     
    #定义队列
    channel.queue_declare(queue='compute_queue')
    print ' [*] Waiting for n'
     
    #将n值加1
    def increase(n):
        return n + 1
     
    #定义接收到消息的处理方法
    def request(ch, method, properties, body):
        print " [.] increase(%s)"  % (body,)
     
        response = increase(int(body))
     
        #将计算结果发送回控制中心
        ch.basic_publish(exchange='',
                         routing_key=properties.reply_to,
                         body=str(response))
        ch.basic_ack(delivery_tag = method.delivery_tag)
     
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(request, queue='compute_queue')
     
    channel.start_consuming()
  • 相关阅读:
    二分图之最小边覆盖(poj3020)
    第一章:计算机网络概述
    X Window 简单的新手教程
    SharePoint Permission Analyzer 权限分析仪
    《源创新》:破坏性创新换了个说法,有陷入锤子钉子模式的嫌疑,书中的案例可以看一看。
    《金融可以颠覆历史》:隐藏在历史事件背后的金融制度发展历程
    转发:三伏天话“三伏贴”
    《浪潮之巅》(第2版):精彩的IT商战史
    《史玉柱自述》:管理者要谦虚,好的经营策略是试出来的
    《生活中的经济学》:主张让市场去解决生活中的问题,离中国的现实有点远
  • 原文地址:https://www.cnblogs.com/SR-Program/p/12655728.html
Copyright © 2011-2022 走看看