asyncio + multiprocessing + unix
First, you should consider using loop.run_in_executor with a ProcessPoolExecutor if you plan to run python subprocesses from within the loop. As for your problem, you can use the event loop policy functions to set a new loop:
import asynciofrom concurrent.futures import ProcessPoolExecutorasync def sub_main(): print('Hello from subprocess')def sub_loop(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(sub_main())async def start(executor): await asyncio.get_event_loop().run_in_executor(executor, sub_loop)if __name__ == '__main__': executor = ProcessPoolExecutor() asyncio.get_event_loop().run_until_complete(start(executor))
You should always add a check to see how you're running the code (the if __name__ == '__main__':
part. Your subprocess is running everything in the module a 2nd time, giving you grief (couldn't resist).
import asyncio, multiprocessingasync def sub_main(): print('Hello from subprocess')def sub_loop(): asyncio.get_event_loop().run_until_complete(sub_main())async def start(): multiprocessing.Process(target=sub_loop).start()if __name__ == '__main__': asyncio.get_event_loop().run_until_complete(start())