Message 393726 - Python tracker (original) (raw)

It's even slightly easier for tornado, which can reasonably set the proactor-wrapper policy at IOLoop start time, which means asyncio.get_event_loop() returns a loop with add_reader. But pyzmq doesn't get invoked until an event loop is already running.

That's not what I'm doing in Tornado; I don't change the policy or the result of get_event_loop. Instead, I call get_event_loop (only once) and wrap its result in AddThreadSelectorEventLoop. This works even while the event loop is already running (which is not an uncommon case; there is no expectation that you use tornado.ioloop.IOLoop.start instead of asyncio.EventLoop.run_forever).

This relies on the fact that I already have my own thread-local lookup function to retrieve the wrapped event loop; an application that used the more typical asyncio patterns and relied on get_event_loop would indeed have difficulty with this pattern.