相关疑难解决方法(0)

为Pika ioloop异步设置超时(RabbitMQ)

我需要能够优雅地阻止在Pika ioloop中工作的消费者(工人).工人应该在60秒后停止.当前处理的消息应该完成.

我试图connection.close()在回调函数中放一个但是只停止当前线程而不是完整的ioloop.它给出了一个可怕的错误输出.

请参阅我的代码中的第16行及以下内容:我使用了(关于Pika ioloop的基本示例http://pika.github.com/connecting.html#cps-example:

    from pika.adapters import SelectConnection
    channel = None
    def on_connected(connection):
        connection.channel(on_channel_open)

    def on_channel_open(new_channel):
        global channel
        channel = new_channel
        channel.queue_declare(queue="test", durable=True, exclusive=False, auto_delete=False, callback=on_queue_declared)

    def on_queue_declared(frame):
        channel.basic_consume(handle_delivery, queue='test')

    def handle_delivery(channel, method, header, body):
        print body

        # timer stuff which did NOT work
        global start_time, timeout, connection
        time_diff = time.time()-start_time
        if time_diff > timeout:
            #raise KeyboardInterrupt
            connection.close()

    timeout = 60
    start_time = time.time()

    connection = SelectConnection(parameters, on_connected)

    try:
        connection.ioloop.start()
    except KeyboardInterrupt:
        connection.close()
        connection.ioloop.start()
Run Code Online (Sandbox Code Playgroud)

python rabbitmq pika

4
推荐指数
1
解决办法
6824
查看次数

标签 统计

pika ×1

python ×1

rabbitmq ×1