liveBook · Manning

Requests

As explained in Python Concurrency with asyncio

Requests is a Python library for making HTTP requests synchronously.

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

How Python Concurrency with asyncio covers it

How deep it goes
Explains installation, GET requests, status codes, sessions, event-loop blocking, and thread-based integration without detailing thread-pool implementation.
Examples
Demonstrates repeated example.com status-code requests and three asyncio tasks that execute blocking calls serially.
What it assumes
Assumes basic Python functions and asyncio concepts while teaching installation, HTTP GET calls, response status codes, and blocking behavior.
What you can do after
Apply it to make synchronous HTTP requests and run those calls concurrently through asyncio-managed threads.
How it connects
Connects HTTP requests with asyncio, event loops, aiohttp, multithreading, and thread pool executors.

Also explained in

Compare how 6 books explain requests →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.