Skip to content
Myr

parallel.md

This file

Path

docs/parallel.md

70 lines · 2.4 KiB · Markdown

Last changed by

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

History of this file

Take it

The walk stopped after 200 commits: lines older than f9e3ad4 point at it.

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

lines 1–4why, one step back
1# Parallel Requests
23!!! warning4 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

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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,

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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:

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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:

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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.

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 51–53why, one step back
51>>> with httpx.Client() as client:
52... with client.parallel() as parallel:53... ...

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

lines 54–61why, one step back
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

Document client block-usage and close() (#487)Florimond Mancabefore adoption074cd25

lines 62–67why, one step back
62>>> async with httpx.AsyncClient() as client:
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()

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

lines 68–70why, one step back
68```
6970See [the Async Client documentation](async.md) for more details.