Changes to async event loops in Python 3.10
In Python 3.10, there a few changes to the asyncio
library that have caused some issues in our Complete Python Course.
We were using asyncio.get_event_loop()
, and using its return value to execute some async tasks. Now, calling get_event_loop()
is deprecated and instead you should use asyncio.run()
.
Here's the code that the course shows for asynchronously scraping a few websites:
import aiohttp
import asyncio
import time
async def fetch_page(url):
page_start = time.time()
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
print(f"Page took {time.time() - page_start}")
return response.status
loop = asyncio.get_event_loop()
tasks = [fetch_page("http://books.toscrape.com") for i in range(10)]
start = time.time()
loop.run_until_complete(asyncio.gather(*tasks))
print(f"All took {time.time() - start}")
But now, Python emits warnings because asyncio.get_event_loop()
is deprecated.
Updates to the official Python documentation show us a new, better way of doing this:
import aiohttp
import asyncio
import time
async def fetch_page(url):
page_start = time.time()
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
print(f'Page took {time.time() - page_start}')
async def main():
tasks = [fetch_page('http://books.toscrape.com') for i in range(10)]
await asyncio.gather(*tasks)
start_time = time.time()
asyncio.run(main())
print(f'All took {time.time() - start_time}')
There are two main changes:
- Define a "main" coroutine that is the entry point to your asynchronous code. Here we are doing
async def main()
. - Instead of calling
get_event_loop
andrun_until_complete
, just callasyncio.run(main())
.
This is much more straightforward because the developer doesn't have to worry about getting an event loop, or which event loop is running. Just calling .run()
does everything for you.
If you are following along with our Complete Python Course and you've come across this problem, I hope this post helps! And if you'd like to learn more about Python and you aren't enrolled in our course, consider giving it a try!
Post photo by David Latorre Romero