Skip to main content

select_ok semantics in python

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bombardx-0.1.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bombardx-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (443.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

bombardx-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (429.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

bombardx-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (379.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

bombardx-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl (392.9 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file bombardx-0.1.0.tar.gz.

File metadata

  • Download URL: bombardx-0.1.0.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.5

File hashes

Hashes for bombardx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 78034f1a55cdeaf6d049dae0bb02cd14efa9326fc5ed8d57210be42f37dc2202
MD5 525dcdc377578d44da0f3ff15e08b0c3
BLAKE2b-256 0d9b46934055490b5c48aecb00281bdec1bb9e3f9c3961d0fe4cca73e4ebd014

See more details on using hashes here.

File details

Details for the file bombardx-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for bombardx-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a28d3429a200fa193a16a2ea8f58fb302453b5e3099ef2228950a639b0d74d75
MD5 6c2d4059cdcc317fbe28ccd218b4e956
BLAKE2b-256 cd388431e35ce34ba8c38997be7ace18d781e790e7f43bbb1c59ec1d8c6c6be0

See more details on using hashes here.

File details

Details for the file bombardx-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for bombardx-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0c6d0f0a87e6b0b503492022ca53640cf38c4633674eb19a791586b72380fc5f
MD5 b5c6cf3d5f13eaa249561b1984169055
BLAKE2b-256 6872fd02599f190c84e8e389b30ca0533d09ea6990081d805cb0b4e216822536

See more details on using hashes here.

File details

Details for the file bombardx-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bombardx-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c9e8a9806c812674c4d7bdb700db09f5cc595e3f6289710c8e9908f41f2ad0d5
MD5 959b61afe82657ca3932263447eecdf1
BLAKE2b-256 c2e547627fcc9210b895c270d79b954b78e4cd8c05cc118ef990df7cbd4ab786

See more details on using hashes here.

File details

Details for the file bombardx-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for bombardx-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6ebcc8cf613ae18508f1c271a3fd4afd9c73b200e5d51cc1d7660327181dd4c6
MD5 acdc76e2dcf26e4aa539a78a3c702c64
BLAKE2b-256 73d247073e2f3fc05028ed83bad72f97f5a10d17591ece5a793f3bebfa31718c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page