How to get the reactor from ApplicationRunner in autobahnPython

杀马特。学长 韩版系。学妹 提交于 2019-12-11 05:18:20

问题


I have an autobahn client using the ApplicationRunner class from autobahn to connect to a WAMP router (crossbar). In the main section it attaches my ApplicationSession class "REScheduler" like this:

if __name__ == '__main__':
    from autobahn.twisted.wamp import ApplicationRunner

    runner = ApplicationRunner(url=u"ws://localhost:8080/ws", realm=u"RE_acct")

    runner.run(REScheduler, start_reactor=True, auto_reconnect=True)

Now I need the reactor that the application runner started for other purposes as well. Like e.g. to call some reactor.callLater(...). How can I access this reactor. I did not find anything in the docs.


回答1:


Twisted (sadly) uses a (process) global reactor object. That means, once a reactor is chosen (which ApplicationRunner does if you set start_reactor=True), simply do a from twisted.internet import reactor at the place within your code where you need it.

asyncio has properly encapsulated the event loop (you can have multiple event loops in a single process).

txaio provides a convenience method that will work on both (it will expose the single, global reactor in Twisted, and it will expose the event loop under which ApplicationRunner is started): txaio.config.loop = reactor



来源:https://stackoverflow.com/questions/39792761/how-to-get-the-reactor-from-applicationrunner-in-autobahnpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!