Triggering Events In Twisted From Another Thread
I have an application which, for convenience (I am reusing existing code) has been split into two different threads: one thread running the twisted reactor another thread running
Solution 1:
You should avoid using threads for this. See User interaction in twisted process for information about how to accept user input in a single thread.
Apart from that, use reactor.callFromThread any time you want to call any Twisted API from a non-reactor thread.
Solution 2:
I actually ran into this problem myself with Twisted. Thankfully after a lot of googling I was able to come up with this answer, works quite well actually! -
defmy_function(s):
do_something_with_s
classGetCommands():
defstart(self, callable):
self.callable = callable
self.startReceiving()
defstartReceiving(self, s = ''):
self.callable(s)
if s != 'exit':
threads.deferToThread(raw_input,' >>> ').addCallback(self.startReceiving)
Then in the main -
getCmds = GetCommands()
reactor.callWhenRunning(getCmds.start, my_function)
reactor.listenTCP(PORT, factory)
reactor.run()
Post a Comment for "Triggering Events In Twisted From Another Thread"