Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用RabbitMQ Pika库实现Python异步流控制_Python_Python 3.x_Asynchronous_Rabbitmq_Pika - Fatal编程技术网

如何使用RabbitMQ Pika库实现Python异步流控制

如何使用RabbitMQ Pika库实现Python异步流控制,python,python-3.x,asynchronous,rabbitmq,pika,Python,Python 3.x,Asynchronous,Rabbitmq,Pika,我有一些基于Pika(0.10.0)提供的异步示例使用者的Python 3.51代码,用于与RabbitMQ(3.6.2)通信() 当前,队列创建接受一个回调,该回调绑定队列。绑定队列还接受它在成功时调用的回调。这已经是我想用某种瀑布流控制代替嵌套回调的地方了,这在使用javascript之类的东西时很典型 此外,我有一个场景,其中一条消息可能会导致创建多个队列,我希望在创建所有队列后确认该消息。这将再次适用于某种流控制,一旦所有队列创建序列并行完成,就会调用函数 现在,我通过混合使用嵌套函数和

我有一些基于Pika(0.10.0)提供的异步示例使用者的Python 3.51代码,用于与RabbitMQ(3.6.2)通信()

当前,队列创建接受一个回调,该回调绑定队列。绑定队列还接受它在成功时调用的回调。这已经是我想用某种瀑布流控制代替嵌套回调的地方了,这在使用javascript之类的东西时很典型

此外,我有一个场景,其中一条消息可能会导致创建多个队列,我希望在创建所有队列后确认该消息。这将再次适用于某种流控制,一旦所有队列创建序列并行完成,就会调用函数

现在,我通过混合使用嵌套函数和闭包来实现这一点。这是可行的,但是如果可能的话,一些带有流量控制的东西会更干净

我将如何在Python中使用流控制实现这一点?下面是我当前代码的简化版本

def on_message(self, unused_channel, basic_deliver, properties, body):
    routing_key_components = basic_deliver.routing_key.split('.')
    queue_counter = len(routing_key_components) - 2

    def queues_setup_complete():
        nonlocal queue_counter
        queue_counter -= 1
        if queue_counter == 0:
            self._channel.basic_publish('bar_ex', basic_deliver.routing_key, body, properties)
            self.acknowledge_message(basic_deliver.delivery_tag)

    for i in range(2, len(routing_key_components)):
        sub_key = routing_key_components[i]
        routing_pattern = 'foo.%s' % sub_key
        queue_name = 'foo_%s' % sub_key
        exchange_name = 'bar_ex' % sub_key
        self._channel.queue_declare(self.on_foo_queue_declareok(routing_pattern, 
                                                                exchange_name,
                                                                queues_setup_complete),
                                    queue_name)

def on_foo_queue_declareok(self, routing_pattern, 
                           exchange_name, queues_setup_complete):
    # nested function with closure returned to allow passing additional parameters to the callback function
    def queue_declared(method_frame):
        self._channel.queue_bind(self.on_foo_queue_bindok(queues_setup_complete),
                                 method_frame.method.queue,
                                 exchange_name, routing_pattern)
    return queue_declared

def on_foo_queue_bindok(self, queues_setup_complete):
    # nested function with closure returned to allow passing the 'queue_setup_completed' function
    # to the callback.
    def foo_bound(unused_frame):
        queues_setup_complete()

    return foo_bound