Skip to main content

Async fanout pattern

Fan out N independent SDK calls in parallel, bound the concurrency so you do not overwhelm the tenant, and get the results back in order. This is the shape that survives outside a notebook.

import asyncio
import sasvi


async def hydrate_alerts(client, alert_ids, max_concurrent=20):
sem = asyncio.Semaphore(max_concurrent)

async def one(alert_id):
async with sem:
return await client.alerts.get_async(alert_id=alert_id)

return await asyncio.gather(*(one(aid) for aid in alert_ids))


async def main():
async with sasvi.SVIClient.from_env() as client:
alert_ids = [...]
alerts = await hydrate_alerts(client, alert_ids)
for a in alerts:
print(repr(a) if a else "miss")


asyncio.run(main())

The semaphore caps the number of outstanding requests. asyncio.gather returns the results in input order, with None wherever the lookup missed.

Tolerate per-item failures​

Use return_exceptions=True so one failure does not cancel the whole batch:

results = await asyncio.gather(
*(one(aid) for aid in alert_ids),
return_exceptions=True,
)
for aid, res in zip(alert_ids, results):
if isinstance(res, BaseException):
log_error(aid, res)
else:
process(res)

Bound the deadline​

Pair concurrency with a per-call deadline so one stuck tenant call cannot pin the whole batch:

client = sasvi.SVIClient(
auth=auth,
config=sasvi.SVIConfig(
base_url=base_url,
max_retries=3,
request_deadline_seconds=10.0,
),
)

When the budget runs out the call raises DeadlineExceededError.

On the async path the deadline is a hard wall-clock cap: the attempt is cancelled when the budget runs out, whatever phase it is in. The sync path caps every connect, pool, write, and read wait at the budget left when the attempt starts, and starts no retry after the deadline. A response that keeps trickling bytes, or stalls in several phases in turn, can still overrun it there. A per-call timeout= never extends the budget.

Choosing max_concurrent​

WorkloadSuggested cap
Local laptop, dev tenant5 to 10
Production read fanout20 to 50, tested against your own tenant
Bulk write or disposition pathMatch the tenant's documented limit; 5 to 10 by default

Every service of one client shares one connection pool, which allows 100 connections by default. Raise it with SVIClient(..., limits=httpx2.Limits(max_connections=200)) when you go higher.

Go further​