I've been having a hard time understanding Python's asyncio module and how to not block on asynchronous calls. For instance, given this code snippet:
import aiohttp
import asyncio
import async_timeout
async def fetch(session, url):
with async_timeout.timeout(10):
async with session.get(url) as response:
return await response.text()
async def main(loop):
print(1)
async with aiohttp.ClientSession(loop=loop) as session:
html = await fetch(session, 'http://python.org')
print(html)
print(2)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
I would expect, similar to how this would work in Javascript, the output to be
1
2
<!doctype html>
<...>
Instead, the function prints 1, blocks until it gets back the html, then prints 2. Why does it block, and how / can I avoid the blocking? Thanks for your help.