Search code examples
pythontwistedautobahncrossbar

How to get the reactor from ApplicationRunner in autobahnPython


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.


Solution

  • 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