我有一个应用程序,为方便起见(我正在重用现有代码),它被分成两个不同的线程:
我想从交互式菜单中执行的任务之一是与反应器进行交互。一旦用户给出了特定的命令,我想触发一个扭曲的事件。这是我的代码的一个非常简化的版本:
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()
如您所见,我展示了两个不同的版本:
运行version2会得到以下结果:
Connecting to 127.0.0.1:8889
gotObject > <twisted.spread.pb.RemoteReference instance at 0x88e734c>
这就是我所期待的。
运行版本1会给出:
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,显示错误信息。
我认为问题出现是因为我在两个不同的线程中共享一个对象,我试图从非扭曲的线程触发扭曲的事件。我希望,由于对象是共享的,反应堆会意识到任何对该物体的影响。
所以我的主要问题是:如何从另一个线程触发扭曲事件?
答案 0 :(得分:2)
你应该避免使用线程。有关如何在单个线程中接受用户输入的信息,请参阅User interaction in twisted process。
除此之外,只要您想从非反应堆线程调用任何Twisted API,就可以使用reactor.callFromThread。
答案 1 :(得分: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()