Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
Python 扭曲是阻塞_Python_Python 2.7_Asynchronous_Rabbitmq_Twisted - Fatal编程技术网

Python 扭曲是阻塞

Python 扭曲是阻塞,python,python-2.7,asynchronous,rabbitmq,twisted,Python,Python 2.7,Asynchronous,Rabbitmq,Twisted,我遇到了一个问题,即我使用twisted库从不同队列设置了多个消费者。这一切都很好,工作正常,但是它似乎并没有继续执行主程序,而是在我调用reactor.run()之后,它只是坐着等待,从不点击我的打印语句 以下是我的主要代码,我在其中实际调用了消费者: cc = InitSetup() AsyncConsumerSetup(DeviceFeaturesConsumer('outboundMessaging', 'messaging', 'outbound', ws, 'registeredCa

我遇到了一个问题,即我使用twisted库从不同队列设置了多个消费者。这一切都很好,工作正常,但是它似乎并没有继续执行主程序,而是在我调用
reactor.run()之后,它只是坐着等待,从不点击我的打印语句

以下是我的主要代码,我在其中实际调用了消费者:

cc = InitSetup()
AsyncConsumerSetup(DeviceFeaturesConsumer('outboundMessaging', 'messaging', 'outbound', ws, 'registeredCallback', cc, connection), cc)
reactor.run()
print "starting to receive device"
ws.client.on('newDevice', receive_device)
print "end receive device"
以下是方法
InitSetup
AsyncConsumerSetup

def InitSetup():
    parameters = ConnectionParameters()
    cc = protocol.ClientCreator(reactor,
                                TwistedProtocolConnection,
                                parameters)
    return cc


def AsyncConsumerSetup(consumer, cc):
    try:
        d = cc.connectTCP("127.0.0.1", 5672)
        d.addCallback(lambda protocol: protocol.ready)
        d.addCallback(consumer.on_connected)
        d.addErrback(log.err)
    except Exception as e:
        print e

我对twisted的理解可能是错误的,但是我会认为print语句和
ws.client.on
会触发。

reactor.run()
之后不会运行任何东西,这是一个事件循环(外行术语中的while循环)。这就是代码阻塞的方式。其思想是让所有代码在事件循环中执行。一种方法是将执行使用者的代码放入函数中,移除
reactor.run()
,并在
reactor
启动时调用它

def InitSetup():
    parameters = ConnectionParameters()
    cc = protocol.ClientCreator(reactor,
                                TwistedProtocolConnection,
                                parameters)
    return cc


def AsyncConsumerSetup(consumer, cc):
    try:
        d = cc.connectTCP("127.0.0.1", 5672)
        d.addCallback(lambda protocol: protocol.ready)
        d.addCallback(consumer.on_connected)
        d.addErrback(log.err)
    except Exception as e:
        print e

def initFunction():
    cc = InitSetup()
    AsyncConsumerSetup(DeviceFeaturesConsumer('outboundMessaging', 'messaging', 'outbound', ws, 'registeredCallback', cc, connection), cc)
    print "starting to receive device"
    ws.client.on('newDevice', receive_device)
    print "end receive device"

reactor.callWhenRunning(initFunction)
reactor.run()

这将是你的主要模块。不幸的是,我对您的代码了解不够,无法对其进行测试。

是的,您对Twisted的理解是错误的,但每个人在学习时都会遇到这种情况。一切都必须在
reactor
循环中运行。notrorious,我该如何将代码移动到reactor中。基本上,您必须确保在
reactor.run()之前执行所有逻辑。因此,如果将
reactor.run()
移动到末尾,它应该可以正常工作。