bombard
Python bindings for rust's select_ok macro.
Motivation
Async Python currently lacks primitives for polling coroutines until the first success. Bombard is useful in situations where users spawn concurrent tasks which may err, but only care that at least one of them completes.
Coroutines heavily dependent on network-induced latencies (e.g. gen-ai applications which aim for fault tolerance through concurrent http retries) can benefit from bombardx.select_ok() to reduce wait times.
asyncio.wait gets close, but is unable to distinguish between an exception and a successful result as it simply returns the result of the first future completes:
# some bad code
import asyncio
async def ok(i: int):
await asyncio.sleep(i)
print("ok")
async def err(i: int):
await asyncio.sleep(i)
raise RuntimeError("oops")
async def main():
try:
tasks = [fail(1), ok(2)]
coroutines = map(asyncio.Task, tasks) # <- fail(1) completes before ok(2) !
done, pending = await asyncio.wait(coroutines, return_when=asyncio.FIRST_COMPLETED)
except RuntimeError as e:
raise e
asyncio.run(main()) # raises a RuntimeError !
You could roll your own select_ok by repeatedly awaiting asyncio.wait and updating tasks with pending until you've exhausted the list, but this is not ideal.
Contrast the code above with :
import asyncio
from bombardx import select_ok
async def main():
try:
done = await select_ok(fail(1), ok(2)) # <- select first successful
except RuntimeError as e:
raise e
asyncio.run(main()) # ignores runtime error and prints "ok" after 2 seconds
Bombard also abstracts select_ok through a @bombard(num = ...) decorator you can add to a fallible async function specifying the desired concurrency level :
import random
from bombardx import bombard
async def random_sleep():
"""sleeps for a random amount of time and returns the duration"""
t = random.randint(1, 10)
await asyncio.sleep(t)
return t
@bombard(num=10)
async def fallible():
t = await random_sleep()
if random.random() < 0.5:
return t
raise RuntimeError(t)
Metadata
Release files for bombardx 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bombardx-0.1.0.tar.gz | 19.3 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| bombardx-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| bombardx-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| bombardx-0.1.0-cp310-abi3-macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 11.0+ ARM64 | Details |
| bombardx-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl | CPython 3.10 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 1.7 MB
Release files / bombardx-0.1.0.tar.gz
| Download URL | bombardx-0.1.0.tar.gz |
|---|---|
| Size | 19.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
78034f1a55cdeaf6d049dae0bb02cd14efa9326fc5ed8d57210be42f37dc2202
|
|
BLAKE2b-256 checksum How to use checksums |
0d9b46934055490b5c48aecb00281bdec1bb9e3f9c3961d0fe4cca73e4ebd014
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.5
|
Release files / bombardx-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | bombardx-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 443.0 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
a28d3429a200fa193a16a2ea8f58fb302453b5e3099ef2228950a639b0d74d75
|
|
BLAKE2b-256 checksum How to use checksums |
cd388431e35ce34ba8c38997be7ace18d781e790e7f43bbb1c59ec1d8c6c6be0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.5
|
Release files / bombardx-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | bombardx-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 429.0 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
0c6d0f0a87e6b0b503492022ca53640cf38c4633674eb19a791586b72380fc5f
|
|
BLAKE2b-256 checksum How to use checksums |
6872fd02599f190c84e8e389b30ca0533d09ea6990081d805cb0b4e216822536
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.5
|
Release files / bombardx-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | bombardx-0.1.0-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 379.8 kB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
c9e8a9806c812674c4d7bdb700db09f5cc595e3f6289710c8e9908f41f2ad0d5
|
|
BLAKE2b-256 checksum How to use checksums |
c2e547627fcc9210b895c270d79b954b78e4cd8c05cc118ef990df7cbd4ab786
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.5
|
Release files / bombardx-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
| Download URL | bombardx-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 392.9 kB |
| Tags | CPython 3.10 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
6ebcc8cf613ae18508f1c271a3fd4afd9c73b200e5d51cc1d7660327181dd4c6
|
|
BLAKE2b-256 checksum How to use checksums |
73d247073e2f3fc05028ed83bad72f97f5a10d17591ece5a793f3bebfa31718c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.5
|