RabbitMQ,Pika和Twisted Reactor

时间:2017-11-09 20:34:39

标签: python-2.7 rabbitmq twisted

我在运行发布商发布一条消息时遇到停止反应堆的问题。似乎reactor.stop()函数在实际发送消息之前执行并在队列中生成。逻辑上我认为这是有道理的,但它会在消息进入队列之前停止代码。当我取出reactor.stop()时,消息进入队列,但此时反应器根本无法停止。

@inlineCallbacks
def do_msg(connection):
    try:
        msg = content
        print "Connected to broker."

        chan = yield connection.channel()
        print "Channel constructed."

        # Declare exchange
        yield chan.exchange_declare(exchange="test_exchange", 
                  exchange_type="topic", durable=True)
        print "Exchange opened"

        # Declare queue
        yield chan.queue_declare(queue="test_queue", durable=True, 
                   exclusive=False)
        print "Queue opened."

        # Bind exchange and queue
        yield channel.queue_bind(exchange="test_exchange", 
                   queue="test_queue", routing_key="test_key")
        print "Exchange and Queue bound"

        print "Sending message: %s" % msg
        yield chan.basic_publish(exchange="test_exchange", body=msg, 
                 routing_key="test_key",                               
                 properties=BasicProperties(delivery_mode = 2))

    except Exception, e:
        print "ERROR: %s" % str(e)
    finally:
        yield connection.close()
        reactor.stop() # problem occurs here

有谁知道解决这个问题的方法?有没有办法根据消息到达代理的条件停止反应堆?

0 个答案:

没有答案