[英]Asyncio event loop per python process (aioprocessing, multiple event loops)
I have two processes; 我有两个过程; a main process and a subprocess.
主进程和子进程。 The main process is running an
asyncio
event loop, and starts the subprocess. 主进程正在运行
asyncio
事件循环,并启动子进程。 I want to start another asyncio event loop in the subprocess. 我想在子进程中启动另一个asyncio事件循环。 I'm using the
aioprocessing
module to launch the subprocess. 我正在使用
aioprocessing
模块来启动子aioprocessing
。
The subprocess function is: 子进程函数是:
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: 但是我收到一个错误:
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 the existing, asyncio
event loop in the subprocess? 是否可以在子
asyncio
启动新的或重新启动现有的asyncio
事件循环? If so, how? 如果是这样,怎么样?
Sorry for disturb! 对不起打扰! I 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 new asycnio event loop inside of subprocess started from process with asyncio event loop 把这段代码用asyncio事件循环从进程启动的子进程内部启动新的asycnio事件循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.