Requests is a popular Python HTTP client for making synchronous web requests. Its calls are blocking: each requests.get call stops its thread until the response finishes. section 7.2.1
Use requests directly for straightforward synchronous HTTP work:
- Install it with pip and call requests.get(url) to receive a response object. section 7.2.1
- Read response.status_code to inspect the HTTP result; sessions can persist cookies across requests. section 7.2.1
- Avoid calling it directly inside an asyncio coroutine, because it blocks the event loop and prevents other tasks from running. section 2.7.2
- Run blocking requests operations in threads or a thread pool when combining them with asyncio. section 7.0
Retrieve a status code
import requests def get_status_code(url: str) -> int: response = requests.get(url) return response.status_code print(get_status_code('https://www.example.com')) section 7.2.1
Listing 2.20 Incorrectly using a blocking API in a coroutine
import asyncio
import requests
from util import async_timed
@async_timed()
async def get_example_status() -> int:
return requests.get('http:/ / www .example .com').status_code
@async_timed()
async def main():
task_1 = asyncio.create_task(get_example_status())
task_2 = asyncio.create_task(get_example_status())
task_3 = asyncio.create_task(get_example_status())
await task_1
await task_2
await task_3
asyncio.run(main())
Compare the apparent concurrency of three asyncio tasks with their serialized execution when each task calls blocking requests.get. section 2.7.2





