从另一个线程触发Twisted事件
我有一个应用程序,为了方便(我在重复使用现有代码),把它分成了两个不同的线程:
- 一个线程在运行 twisted reactor
- 另一个线程在运行一个互动菜单
我想在互动菜单中做的一件事是与 reactor 进行交互。当用户输入特定命令时,我想触发一个 twisted 事件。下面是我代码的一个非常简化的版本:
from twisted.spread import pb
from twisted.internet import reactor
import threading
class TaskGatewaySupport():
def __init__(self):
self.object = None
self.factory = pb.PBClientFactory()
self.connector = None
def gotObject(self, object):
print 'gotObject > %s' % object
self.object = object
return object
def gotData(self, data):
return data
def gotNoObject(self, reason):
print 'gotNoObject > no object: %s' % reason
def connect(self, task_gateway_host = '127.0.0.1', task_gateway_pb_port = 8889):
print 'Connecting to %s:%s' % (task_gateway_host, task_gateway_pb_port)
self.connector=reactor.connectTCP(task_gateway_host, task_gateway_pb_port, self.factory)
d = self.factory.getRootObject()
d.addCallbacks(self.gotObject, self.gotNoObject)
return d
def Menu(task_gateway_support):
while True:
print '''
A) Connect
'''
choice = raw_input('Option > ')
if choice == 'A' : task_gateway_support.connect()
else : print "ERR: command not yet supported"
def version1():
task_gateway_support = TaskGatewaySupport()
thread = threading.Thread(target = Menu, args = (task_gateway_support,))
thread.start()
reactor.run()
def version2():
task_gateway_support = TaskGatewaySupport()
d = task_gateway_support.connect()
reactor.run()
if __name__ == '__main__':
version1()
你可以看到,我展示了两个不同的版本:
- version1 是我想运行的,但它没有成功
- version2 只有一个线程,而且没有互动
运行 version2 会得到这个结果:
Connecting to 127.0.0.1:8889
gotObject > <twisted.spread.pb.RemoteReference instance at 0x88e734c>
这是我预期的结果。
运行 version1 会得到这个:
A) Connect
Option > A
Connecting to 127.0.0.1:8889
A) Connect
Option > ^CgotNoObject > no object: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectError'>: An error occurred while connecting: [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion: Connection lost.
].
]
我在这里选择了选项 A,但因为没有任何反应,我按了 ^C,这时显示了错误信息。
我认为问题出现在我在两个不同的线程中共享一个对象,并且我试图从非 twisted 线程触发 twisted 事件。我原本希望,由于这个对象是共享的,reactor 会知道这个对象发生了什么。
所以我主要想问的是:我该如何从另一个线程触发 twisted 事件呢?
2 个回答
0
我自己也遇到过这个问题,使用的是Twisted。幸好经过大量的搜索,我找到了这个解决办法,效果还不错!-
def my_function(s):
do_something_with_s
class GetCommands():
def start(self, callable):
self.callable = callable
self.startReceiving()
def startReceiving(self, s = ''):
self.callable(s)
if s != 'exit':
threads.deferToThread(raw_input,' >>> ').addCallback(self.startReceiving)
然后在主程序中 -
getCmds = GetCommands()
reactor.callWhenRunning(getCmds.start, my_function)
reactor.listenTCP(PORT, factory)
reactor.run()
2
你应该避免在这个情况下使用线程。想了解如何在单线程中接受用户输入,可以查看这个链接:在扭曲进程中的用户交互。
除此之外,每当你想从一个非反应线程调用任何Twisted的API时,记得使用reactor.callFromThread。