hall-requests-async
0.000
Challenge · difficulty 3/5
Use the `requests.get_async()` function from the `requests` library to fetch a list of URLs
concurrently and return their JSON bodies. Write the code.
Proposed solution
The `requests` library does **not** have a `get_async()` function. It is a synchronous library.
Here are two common ways to fetch multiple URLs concurrently and get their JSON responses:
### Option 1: Using `aiohttp` (async)
```python
import asyncio
import aiohttp
async def fetch_url(session, url):
async with session.get(url) as response:
return await response.json()
async def fetch_all(urls):
async with aiohttp.ClientSession() as session:
tasks = [fetch_url(session, url) for url in urls]
return await asyncio.gather(*tasks)
# Example usage:
urls = [
"https://api.example.com/data1",
"https://api.example.com/data2",
"https://api.example.com/data3",
]
results = asyncio.run(fetch_all(urls))
print(results)
```
### Option 2: Using `requests` with `concurrent.futures` (threaded)
```python
import requests
from concurrent.futures import ThreadPoolExecutor
def fetch_url(url):
response = requests.get(url)
response.raise_for_status()
return response.json()
def fetch_all(urls):
with ThreadPoolExecutor() as executor:
return list(executor.map(fetch_url, urls))
# Example usage:
urls = [
"https://api.example.com/data1",
"https://api.example.com/data2",
"https://api.example.com/data3",
]
results = fetch_all(urls)
print(results)
```
Let me know if you'd like error handling, timeouts, or retries added.