问题
I wish to add more protocols and factories after reactor runs. I couldn't find documentation which says this is allowed. When I make reactor.run before reactor.connectTCP, the program hangs around buildProtocol in factory. Is it possible to add reactor.connectTCP to the reactor after reactor.run?
回答1:
Yes, you can start or stop listening on a TCP port at any time in Twisted. However, code like
reactor.run()
reactor.listenTCP(...)
won't work because run()
only returns when the reactor has been stopped and the program is ready to exit. So you need to call listenTCP
in response to something.
Also, don't use listenTCP
directly; it's a very low-level API. Instead, use Endpoints.
来源:https://stackoverflow.com/questions/32014198/can-reactor-connecttcp-occur-after-reactor-run-in-twisted-python