# Servers, workers and serverless

The SDK keeps a lock, a connection pool and, for the aggregator and the log handler, a background thread. A process that **forks** copies all of them in whatever state the parent held them, and a copied lock may never be released. The SDK therefore registers with `os.register_at_fork`: in a forked child every client, aggregator and handler gets new locks, empty buffers, a new instance id and no thread (it starts again at the first use). The rules below say where that is enough and where you act.

| Where it runs | What you do |
| --- | --- |
| gunicorn, with or without `preload_app` | Nothing for the SDK. See below. |
| uWSGI | `lazy-apps = true`, or call `watchtower15.reinit_after_fork()` in a `postfork` hook |
| Celery prefork | `install()` from [Celery](https://developer.watchtower15.com/sdks/python/celery/) |
| A custom `http_client` | Build it after the fork, in the child |
| AWS Lambda | `flush(final=True)` before returning, or `flush="per_request"` |
| The free-threaded build | Nothing: it is tested from Python 3.14 |

## gunicorn

With `preload_app = True` the master imports your app, so a client and an aggregator built at import exist in the master and are copied by `fork()`. Python’s at-fork hook renews them in each worker, so each worker has its own connections, buffers and instance id (the SDK’s gunicorn recipe tests two preloaded workers making their own requests). Without `preload_app` each worker imports the app itself and nothing is shared. Do not create a client in the master and send with it from a worker through your own `os.fork()`.

## uWSGI

uWSGI forks without Python’s hooks unless `lazy-apps` is on. Set `lazy-apps = true`, so each worker loads the app itself, or renew by hand where uWSGI says a worker has forked:

```python
import watchtower15
from uwsgidecorators import postfork

@postfork
def renew() -> None:
    watchtower15.reinit_after_fork()
```

## An injected http\_client

A client you pass as `http_client=` holds a connection pool that is not safe to share with a child. Build it, and the `Client` around it, in the child (a gunicorn `post_fork` hook, a Celery `worker_process_init` receiver), or let the SDK build its own.

## AWS Lambda

A Lambda instance can freeze right after it answers, so a thread that would flush at the next minute never runs. **Flush before the function returns.**

```python
from watchtower15.integrations.logging import drain_all
from watchtower15.metrics import Aggregator

metrics = Aggregator(client, allow_list={"orders.created": "count"}, flush_interval=None)

def handler(event, context):
    metrics.increment("orders.created")
    result = work(event)
    metrics.flush(final=True)           # sends the open minute too
    drain_all()                          # for a log handler
    return result
```

`flush(final=True)` sends every bucket, the open one included, and never raises. Each invocation’s delta is sent under its own key. In Django, FastAPI and Flask set `flush="per_request"` and the integration does both after every request. A Lambda function that only forwards log lines, without the SDK, is the [AWS Lambda recipe](https://developer.watchtower15.com/sdks/recipes/aws-lambda/), which also shows how to forward a CloudWatch Logs group.

## The free-threaded build

Python 3.14t runs the SDK with the GIL off: its shared state is guarded by locks, not by the GIL, and the suite runs under `PYTHON_GIL=0` from 3.14. A C extension you load that does not declare support re-enables the GIL for the process; that is the extension’s choice, not the SDK’s.

Source: https://developer.watchtower15.com/sdks/python/deployment/
