• Python自动化之rabbitmq rpc client端代码分析(原创)


    RPC调用client端解析

    import pika
    import uuid
    # 建立连接
    class FibonacciRpcClient(object):
    
    
        def __init__(self):
    
            # 建立建立连接和通道
            self.connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
            self.channel = self.connection.channel()
    
            # exclusive(专有的): Only allow access by the current connection
            result = self.channel.queue_declare(exclusive=True)
    
            # 获取队列名称
            self.callback_queue = result.method.queue
    
            # 接收服务端的回应
            # param on_response:The function for dispatching messages to user, having the signature:
            """
            Start a queue consumer.
            This method asks the server to start a "consumer",
            which is a transient request for messages from a specific queue.
            Consumers last as long as the channel they were declared on, or until the client cancels them.
            """
            self.channel.basic_consume(self.on_response, no_ack=True, queue=self.callback_queue)
    
            # 接收到返回消息的处理方法消息
        def on_response(self, ch, method, props, body):
                if self.corr_id == props.correlation_id:
                    self.response = body
    
        def call(self, n):
                self.response = None
                self.corr_id = str(uuid.uuid4())
    
                """This method publishes a message to a specific exchange.
                The message will be routed to queues as defined by the exchange configuration
                and distributed to any active consumers when the transaction, if any, is committed."""
                self.channel.basic_publish(exchange="",
                                           routing_key="rpc_queue",
                                           properties=pika.BasicProperties(
                                           reply_to=self.callback_queue, correlation_id=self.corr_id),
                                           body=str(n)
                                           )
                # 确认是否有收到消息,没有的话阻塞在这里
                    # Will make sure that data events are processed. Dispatches timer and
                    # channel callbacks if not called from the scope of BlockingConnection or
                    # BlockingChannel callback. Your app can block on this method.
                    # while self.response is None:  # 跟start_consuming相似
                    # 是一个等待消息的阻塞过程,连接的任何消息都可以使它脱离阻塞状态(有点像Ajax的事件等待机制)
                while self.response is None:
                    self.connection.process_data_events()
                return int(self.response)
    
    ssh_rpc = FibonacciRpcClient()
    
    response = ssh_rpc.call(30)
    
    
    
    
                    # Processes(处理) I/O events and dispatches timers and `basic_consume`
                    # callbacks until all consumers are cancelled."""
                    # 循环接收我们的消息,接收之后并执行我们的callback函数
             
                    channel.start_consuming()
  • 相关阅读:
    6 网络爬虫引发的问题及Robots协议
    WEB测试方法总结-笔记(转)
    最全的Http协议、get和post请求的整理
    random()函数的应用---面试
    求两个列表的交集、差集、并集---面试
    python中函数参数传递--引用传递(面试)
    linux重定向命令>和>>---面试
    正则表达式re.findall和re.search的使用---面试
    关于可迭代对象的详解
    sorted()函数排序的灵活运用---面试
  • 原文地址:https://www.cnblogs.com/wspblog/p/5973497.html
Copyright © 2020-2023  润新知