// plain
Parallel requests with Python Aiohttp can be made using the asyncio
library. The following example code shows how to make two requests in parallel:
import asyncio
import aiohttp
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
html1 = await fetch(session, 'http://example.com/1')
html2 = await fetch(session, 'http://example.com/2')
print(html1)
print(html2)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
<html>
<head>
<title>Example 1</title>
</head>
<body>
<h1>Example 1</h1>
</body>
</html>
<html>
<head>
<title>Example 2</title>
</head>
<body>
<h1>Example 2</h1>
</body>
</html>
The code consists of the following parts:
import asyncio
andimport aiohttp
: imports theasyncio
andaiohttp
libraries.async def fetch(session, url)
: defines an asynchronous function that takes a session and a URL as parameters and returns the response text.async def main()
: defines an asynchronous function that makes two requests in parallel using thefetch
function.loop = asyncio.get_event_loop()
: creates an event loop.loop.run_until_complete(main())
: runs themain
function until it is complete.
group: aiohttp
onelinerhub: How to make parallel requests with Python Aiohttp?