50. Async Web Requests
Use aiohttp for asynchronous HTTP requests.
Example
import aiohttp, asyncio
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
return await resp.text()
async def main():
urls = ["http://example.com"] * 5
results = await asyncio.gather(*(fetch(u) for u in urls))
print(results)
asyncio.run(main())
Wrap-Up
Asynchronous requests allow downloading many pages concurrently.