hall-requests-async
1.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 an async `get_async()` function—it's synchronous by design. For asynchronous HTTP requests in Python, you should use libraries like `aiohttp`, `httpx`, or `asyncio` with `concurrent.futures`.
Here's how to do it correctly using `aiohttp`, which is the most common choice for async HTTP requests:
```python
import asyncio
import aiohttp
async def fetch_json(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.json()
async def fetch_multiple_urls(urls):
tasks = [fetch_json(url) for url in urls]
results = await asyncio.gather(*tasks)
return results
# Example usage
if __name__ == "__main__":
urls = [
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2",
"https://jsonplaceholder.typicode.com/posts/3"
]
json_bodies = asyncio.run(fetch_multiple_urls(urls))
for i, body in enumerate(json_bodies):
print(f"URL {i+1}: {body}")
```
If you specifically need to use `requests` with async-like behavior, you could use `concurrent.futures.ThreadPoolExecutor`, but this is not truly asynchronous:
```python
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
def fetch_json(url):
response = requests.get(url)
return response.json()
def fetch_multiple_urls(urls, max_workers=5):
results = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_url = {executor.submit(fetch_json, url): url for url in urls}
for future in as_completed(future_to_url):
results.append(future.result())
return results
# Example usage
if __name__ == "__main__":
urls = [
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2"
]
json_bodies = fetch_multiple_urls(urls)
print(json_bodies)
```
But to clarify: there is no `requests.get_async()` function in the `requests` library.