Skip to content
Myr

parallel.md

This file

Path

docs/parallel.md

71 lines · 2.4 KiB · Markdown

Last changed by

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

History of this file

Take it

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 1–2why, one step back
1# Parallel Requests
2

Docs tweaksTom Christiebefore adoption60dca3d

lines 3–4why, one step back
3!!! warning
4 This page documents some proposed functionality that is not yet released.

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 5why, one step back
5 See [pull request #52](https://github.com/encode/httpx/pull/52) for the

Docs tweaksTom Christiebefore adoption60dca3d

lines 6–7why, one step back
6 first-pass of an implementation.
7

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 8why, one step back
8HTTPX allows you to make HTTP requests in parallel in a highly efficient way,

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 9–18why, one step back
9using async under the hood, while still presenting a standard threaded interface.
1011This has the huge benefit of allowing you to efficiently make parallel HTTP12requests without having to switch out to using async all the way through.1314## Making Parallel Requests1516Let's make two outgoing HTTP requests in parallel:1718```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 19why, one step back
19>>> with httpx.parallel() as parallel:

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 20–30why, one step back
20>>> pending_one = parallel.get('https://example.com/1')
21>>> pending_two = parallel.get('https://example.com/2')22>>> response_one = pending_one.get_response()23>>> response_two = pending_two.get_response()24```2526If we're making lots of outgoing requests, we might not want to deal with the27responses sequentially, but rather deal with each response that comes back28as soon as it's available:2930```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 31why, one step back
31>>> with httpx.parallel() as parallel:

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 32–51why, one step back
32>>> for counter in range(1, 10):
33>>> parallel.get(f'https://example.com/{counter}')34>>> while parallel.has_pending_responses:35>>> r = parallel.next_response()36```3738## Exceptions and Cancellations3940The style of using `parallel` blocks ensures that you'll always have well41defined exception and cancellation behaviours. Request exceptions are only ever42raised when calling either `get_response` or `next_response`, and any pending43requests are cancelled on exiting the block.4445## Parallel requests with a Client4647You can also call `parallel()` from a client instance, which allows you to48control the authentication or dispatch behaviour for all requests within the49block.5051```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 52why, one step back
52>>> client = httpx.Client()

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 53–62why, one step back
53>>> with client.parallel() as parallel:
54>>> ...55```5657## Async parallel requests5859If you're working within an async framework, then you'll want to use a fully60async API for making requests.6162```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 63why, one step back
63>>> client = httpx.AsyncClient()

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 64–71why, one step back
64>>> async with client.parallel() as parallel:
65>>> pending_one = await parallel.get('https://example.com/1')66>>> pending_two = await parallel.get('https://example.com/2')67>>> response_one = await pending_one.get_response()68>>> response_two = await pending_two.get_response()69```7071See [the Async Client documentation](async.md) for more details.