Remix.run Logo
Retr0id 2 days ago

> some of the sync functions can only be called from an async function, because they expect an event loop to be present

I recognise that this situation is possible, but I don't think I've ever seen it happen. Can you give an example?

xg15 2 days ago | parent [-]

Everything that directly interacts with an event loop object and calls methods such as loop.call_soon() [1].

This is used by most of asyncio's synchronization primitives, e.g. async.Queue.

A consequence is that you cannot use asyncio Queues to pass messages or work items between async functions and worker threads. (And of course you can't use regular blocking queues either, because they would block).

The only solution is to build your own ad-hoc system using loop.call_soon_threadsafe() or use third-party libs like Janus[2].

[1] https://github.com/python/cpython/blob/e4e2390a64593b33d6556...

[2] https://github.com/aio-libs/janus