Asyncio event loop on python process (aioprocessing, multiple event loops)

I have two processes; main process and subprocess. In the main process, the asyncio event asyncio runs and the subprocess starts. I want to run another asyncio event loop in a subprocess. I am using the aioprocessing module to start a subprocess.

Subprocess Function:

 def subprocess_code(): loop = asyncio.get_event_loop() @asyncio.coroutine def f(): for i in range(10): print(i) yield from asyncio.sleep(1) loop.run_until_complete(f()) 

But I get an error message:

  loop.run_until_complete(f()) File "/usr/lib/python3.4/asyncio/base_events.py", line 271, in run_until_complete self.run_forever() File "/usr/lib/python3.4/asyncio/base_events.py", line 239, in run_forever raise RuntimeError('Event loop is running.') RuntimeError: Event loop is running. 

Is it possible to start a new or restart an existing asyncio event asyncio in a subprocess? If so, how?

+7
python python-asyncio
source share
1 answer

Excuse for troubling! I have found a solution!

 policy = asyncio.get_event_loop_policy() policy.set_event_loop(policy.new_event_loop()) loop = asyncio.get_event_loop() 

put this code to start a new asycnio event loop inside a subprocess launched from a process with an asyncio event loop

+15
source share

All Articles