How to await on Future in from the different thread?

51 Views Asked by At

I have a code that returns Twisted Deferred and I need to await on that deferred so I can use the usual async-await pattern further. So I am converting the Deferred returned from Twisted to A Future, like this:

future  = twisted_deferred.asFuture(asyncio.get_event_loop())
result = await future

After that, the code hangs on await expression, even though I can debug that the set_result is called on this particular future. After additional debugging, I discovered I am calling await on the Future on Thread1. And in the Deferred result callback(where set_result is called) I am ending up in the MainThread, so I assume this is the problem here. The current event loop is the same as in the Future created, though.

Could someone explain why await is never returned? And how to properly implement the workflow so it actually awaits and returns the result when its ready?

Thanks in advance.

1

There are 1 best solutions below

0
Jean-Paul Calderone On

Almost all Twisted APIs must be used in the thread in which the reactor is running. If an API is safe for use in a different thread it will be clearly marked as such.

The behavior of Twisted APIs when called from a non-reactor thread is unspecified.

If you you have code running in a different thread and you want to use some piece of Twisted functionality from it, you can use reactor.callFromThread or blockingCallFromThread.