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 fileTake it
http3 (#86)Tom Christiebefore adoptionc9747aa
lines 1–2why, one step back1# Parallel Requests2Docs tweaksTom Christiebefore adoption60dca3d
lines 3–4why, one step back3!!! warning4 This page documents some proposed functionality that is not yet released.Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 5why, one step back5 See [pull request #52](https://github.com/encode/httpx/pull/52) for theDocs tweaksTom Christiebefore adoption60dca3d
lines 6–7why, one step back6 first-pass of an implementation.7Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 8why, one step back8HTTPX allows you to make HTTP requests in parallel in a highly efficient way,http3 (#86)Tom Christiebefore adoptionc9747aa
lines 9–18why, one step back9using 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```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 19why, one step back19>>> with httpx.parallel() as parallel:http3 (#86)Tom Christiebefore adoptionc9747aa
lines 20–30why, one step back20>>> 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```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 31why, one step back31>>> with httpx.parallel() as parallel:http3 (#86)Tom Christiebefore adoptionc9747aa
lines 32–39why, one step back32>>> 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 Cancellations39Fix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
lines 40–42why, one step back40The style of using `parallel` blocks ensures that you'll always have a well-defined exception and cancellation behaviors. Request exceptions are only ever41raised 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 back4344## Parallel requests with a Client4546You can also call `parallel()` from a client instance, which allows you toFix typos, spelling issues, and grammar in docs (#426)Kyle Galbraithbefore adoptionb65bce5
line 47why, one step back47control the authentication or dispatch behavior for all requests within thehttp3 (#86)Tom Christiebefore adoptionc9747aa
lines 48–50why, one step back48block.4950```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 51why, one step back51>>> client = httpx.Client()http3 (#86)Tom Christiebefore adoptionc9747aa
lines 52–61why, one step back52>>> 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```pythonRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 62why, one step back62>>> client = httpx.AsyncClient()http3 (#86)Tom Christiebefore adoptionc9747aa
lines 63–70why, one step back63>>> 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.