'How to use multiprocessing.Event in an asyncio event loop?

I am using the multiprocessing module to create a Process. In this child process, it will run an asyncio event loop, and in the parent process, I am not. How can I use syncronization primitives for this pair of processes?

Let's say I want a way for the child process to wait for a message from the parent process. If I wasn't using asyncio, I would use multiprocessing.Event. In the parent process, I create the Event and pass it to the child process when I create it. To use, I would call event.set() in the parent process, and in the child process I would use event.wait().

My understanding is that this wouldn't work in asyncio because Event.wait is blocking, and you can't/shouldn't ever block the asyncio event loop.

How can I write a class that works similarly to multiprocessing.Event, but which has an async-aware wait() method?



Solution 1:[1]

On could wrap the blocking call into a awaitable via loop.run_in_executor() and await it somewhere.

# somewhere else
event = mp.Event()

# main
loop = asyncio.get_event_loop()
f = loop.run_in_executor(None, event.wait)
await f 

# or wrap future in a task and wait for it

pending = loop.create_task(coro)
while pending:
   done, pending = asyncio.wait(pending, timeout=2)
   # do other things with it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1