여러분 안녕하세요. 저는 인터넷 로봇을 만들려고 노력하고 있습니다. 스크립트를 Python 3.4에서 3.5 또는 3.6+로 마이그레이션하는 동안 문제를 만났습니다. 그것은 asyncio 사용하고, 3.4 파이썬에 잘 작동하지만 내가 python3.5로 시작 + 내가 오류를 받았을 때 : 여기python asyncio가 3.4에서 3.5+로 마이 그 레이션
RuntimeError: Cannot run the event loop while another loop is running
코드 체계입니다 :
import multiprocessing as mp
import asyncio
import concurrent.futures
import aiohttp
def create_proccesses(separate_loop_creator, coro):
proccesses = []
for n in range(2):
proc = mp.Process(target=separate_loop_creator, args=(coro,))
proc.start()
proccesses.append(proc)
for p in proccesses:
p.join()
def separate_loop_creator(coro):
sep_loop = asyncio.new_event_loop()
asyncio.set_event_loop(sep_loop)
tasks = [asyncio.async(coro(sep_loop)) for _ in range(100)]
try:
sep_loop.run_until_complete(asyncio.wait(tasks))
sep_loop.close()
except Exception as err:
print(err)
for task in tasks:
task.cancel()
sep_loop.close()
@asyncio.coroutine
def manager(exe, loop):
# some calculations and start coros in several processes
loop.run_in_executor(
exe,
create_proccesses,
separate_loop_creator,
some_coro
)
@asyncio.coroutine
def some_work_in_mainloop():
while True:
print('Some server dealing with connections here...')
yield from asyncio.sleep(1)
@asyncio.coroutine
def some_coro(loop):
with aiohttp.ClientSession(loop=loop) as session:
response = yield from session.get('http://google.com')
yield from asyncio.sleep(2)
print(response.status)
if __name__ == '__main__':
mainloop = asyncio.get_event_loop()
executor = concurrent.futures.ProcessPoolExecutor(5)
asyncio.async(some_work_in_mainloop())
asyncio.async(manager(executor, mainloop))
try:
mainloop.run_forever()
finally:
mainloop.close()
예외가 separate_loop_creator()
코 루틴에 제기하고있다 RuntimeError: Cannot run the event loop while another loop is running
. 나는 get_event_loop()
mechnics를 변경했기 때문에 그런 것 같지만 코드에 무엇이 잘못된 것인지 이해하지 못합니다. 여기
Traceback (most recent call last):
File "tst.py", line 21, in separate_loop_creator
sep_loop.run_until_complete(asyncio.wait(tasks))
File "/root/.pyenv/versions/3.5.3/lib/python3.5/asyncio/base_events.py", line 454, in run_until_complete
self.run_forever()
File "/root/.pyenv/versions/3.5.3/lib/python3.5/asyncio/base_events.py", line 411, in run_forever
'Cannot run the event loop while another loop is running')
RuntimeError: Cannot run the event loop while another loop is running
Cannot run the event loop while another loop is running
Traceback (most recent call last):
File "tst.py", line 21, in separate_loop_creator
sep_loop.run_until_complete(asyncio.wait(tasks))
File "/root/.pyenv/versions/3.5.3/lib/python3.5/asyncio/base_events.py", line 454, in run_until_complete
self.run_forever()
File "/root/.pyenv/versions/3.5.3/lib/python3.5/asyncio/base_events.py", line 411, in run_forever
'Cannot run the event loop while another loop is running')
RuntimeError: Cannot run the event loop while another loop is running
파이썬 3.4.3 결과 :
...
200
Some server dealing with connections here...
200
200
Some server dealing with connections here...
200
200
Some server dealing with connections here...
200
...
1 월 1 일, 아마도 3.6.0 버그뿐 아니라 3.5.3에서 3.6.0까지 동일한 오류 발생 CPython –
예, 버그는 3.6.0에서 3.5.3으로 백 포트되었습니다. 해결 방법을 푸시했습니다. 3.5 및 3.6 오늘 : https://github.com/python/cpython/pull/404 좋은 소식은 파이썬 3.6입니다.1은 3 월 중순에 나옵니다. – 1st1