Windows에서 Python 3.4에서 asyncio 및 aiohttp를 사용하는 https 요청의 경우 2 개의 이벤트 루프를 사용해야합니다. 셸 명령을 실행하는 ProactorEventLoop 및 HTTPS 요청의 기본 이벤트 루프. ProactorEventLoop은 HTTPS 명령에 대해 작동하지 않습니다.Python에서 asyncio 이벤트 루프를 닫으면 예외가 발생합니다.
다음 코드는 새로 만든 기본 이벤트 루프를 사용하고 Windows에서 마지막으로 닫으려고하면 어떻게되는지 보여줍니다.
> Traceback (most recent call last):
> File "C:\BuildUtilities\p3.4env0\lib\site-packages\aiohttp\connector.py", line 56, in __del__
> self.close()
> File "C:\BuildUtilities\p3.4env0\lib\site-packages\aiohttp\connector.py", line 97, in close
> transport.close()
> File "C:\Python34\Lib\asyncio\selector_events.py", line 375, in close
> self._loop.remove_reader(self._sock_fd)
> File "C:\Python34\Lib\asyncio\selector_events.py", line 155, in remove_reader
> key = self._selector.get_key(fd)
> AttributeError: 'NoneType' object has no attribute 'get_key'
그것을 언급하는 것은 예외를 제거하고 그 이유를 모르는 : 아래 그림과 같이 내가 마지막에 loop.close
를 호출하면 나는 말에 예외를 얻을. 하나 밖에없는
import asyncio
import aiohttp
@asyncio.coroutine
def get_body(url):
response = yield from aiohttp.request('GET', url)
return (yield from response.read_and_close())
#loop = asyncio.ProactorEventLoop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
f = asyncio.async(get_body('https://www.google.com'))
try:
loop.run_until_complete(f)
except Exception as e:
print(e)
if f.result():
print(f.result())
loop.close()
감사합니다, greenaj
SelectorEventLoop 닫기가 Windows 7 64 비트에서 더 이상 충돌하지 않는다는 것을 확인했습니다. 'pip install aiohttp --upgrade'는 aiohttp를 0.7.0에서 0.7.2로 업그레이드하면서이 문제를 해결했습니다. 고맙습니다. – greenaj