For https requests using asyncio and aiohttp in Python 3.4 on Windows I'll need to use 2 event loops. A ProactorEventLoop for running shell commands, and the default event loop for HTTPS requests. The ProactorEventLoop does not work for HTTPS commands, unfortunately.
The following code below shows what happens when I use a newly created default event loop and try to close it at the end on Windows. I get exceptions at the end if I call loop.close at the end as shown below:
> 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'
Commenting it out removes the exception and I don't know why. The one and only
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()
Thanks, greenaj