<!-- Source: https://docs.biosimulant.com/developer-api/examples -->

# Python examples

These patterns use the public `biosimulant` distribution.

## Blocking

```python
from biosimulant import Client

with Client() as client:
    result = client.run("demi/microbiology-hello-world-growth@1.0.0", timeout=300)
```

## Async

```python

from biosimulant import AsyncClient

async def main():
    async with AsyncClient() as client:
        result = await client.run(
            "demi/microbiology-hello-world-growth@1.0.0",
            timeout=300,
        )
        print(result.outputs)

asyncio.run(main())
```

## Background run

```python
from biosimulant import Client

with Client() as client:
    run = client.runs.create(
        ref="demi/microbiology-hello-world-growth@1.0.0",
        metadata={"batch": "2026-07-19"},
    )
    save_run_id(run.id)
```

## Cancellation

```python
with Client() as client:
    run = client.runs.retrieve(load_run_id())
    run.cancel()
```

## Timeout recovery

```python
from biosimulant import Client, RunTimeout

with Client() as client:
    run = client.runs.create(ref="demi/microbiology-hello-world-growth@1.0.0")
    try:
        result = run.wait(timeout=10)
    except RunTimeout as exc:
        save_run_id(exc.run.id)  # the server job was not cancelled
```

## Parallel runs

```python

from biosimulant import AsyncClient

async def main():
    async with AsyncClient() as client:
        results = await asyncio.gather(*[
            client.run(
                "demi/microbiology-hello-world-growth@1.0.0",
                inputs={"initial_cells": cells},
                timeout=300,
            )
            for cells in (10, 20, 40)
        ])
        return [result.outputs for result in results]
```

The account's compute-concurrency limit still applies; excess jobs remain queued.

## Webhook verification

```python
from biosimulant import verify_webhook_signature

def receive_webhook(raw_body: bytes, signature: str) -> None:
    verify_webhook_signature(
        raw_body,
        signature,
        secret_from_your_secret_manager(),
    )
    process_event(raw_body)
```

Pass the raw, unmodified request bytes and the `Biosimulant-Signature` header.
