Skip to content
Myr

parallel.md

This file

Path

docs/parallel.md

70 lines · 2.4 KiB · Markdown

Last changed by

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

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–39why, 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 Cancellations39

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

lines 40–42why, one step back
40The style of using `parallel` blocks ensures that you'll always have a well-defined exception and cancellation behaviors. Request exceptions are only ever
41raised when calling either `get_response` or `next_response` and any pending42requests are canceled on exiting the block.

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 43–46why, one step back
43
44## Parallel requests with a Client4546You can also call `parallel()` from a client instance, which allows you to

Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5

line 47why, one step back
47control the authentication or dispatch behavior for all requests within the

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 48–50why, one step back
48block.
4950```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 52–61why, one step back
52>>> with client.parallel() as parallel:
53>>> ...54```5556## Async parallel requests5758If you're working within an async framework, then you'll want to use a fully59async API for making requests.6061```python

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

http3 (#86)Tom Christiebefore adoptionc9747aa

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