> This page is for version Main (default).
> For other versions, use one of these documentation indexes:
> - Main (default): https://docs.nvidia.com/nemo/gym/main/llms.txt
> - 0.7.0: https://docs.nvidia.com/nemo/gym/v0.7.0/llms.txt
> - 0.6.0: https://docs.nvidia.com/nemo/gym/v0.6.0/llms.txt
> - 0.5.1: https://docs.nvidia.com/nemo/gym/v0.5.1/llms.txt
> - 0.5.0: https://docs.nvidia.com/nemo/gym/v0.5.0/llms.txt
> - 0.4.0: https://docs.nvidia.com/nemo/gym/v0.4.0/llms.txt
> - 0.3.0: https://docs.nvidia.com/nemo/gym/v0.3.0/llms.txt
> - 0.2.1: https://docs.nvidia.com/nemo/gym/v0.2.1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/gym/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/gym/_mcp/server.

# nemo_gym.server_runtime.uvicorn_reuse_port

Run a multi-worker Uvicorn server whose workers each listen on their own SO_REUSEPORT socket.

Uvicorn's multi-worker mode hands every worker the same listening socket, and whichever worker
wakes first accepts the next connection. Under load that spreads connections unevenly, and since
clients keep connections alive, a few workers end up serving most of the traffic while the others
idle. Here each worker binds its own socket to the server's address with SO_REUSEPORT, so the
Linux kernel spreads new connections evenly across the workers' sockets.

The main process keeps Uvicorn's usual role (spawning, health-checking and restarting workers). Its
own socket reserves the address, also while workers are still starting, and is never listened on, so
it takes no connections.

## Module Contents

### Classes

| Name                                                                                             | Description                                                                                |
| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| [`_ReusePortUvicornConfig`](#nemo_gym-server_runtime-uvicorn_reuse_port-_ReusePortUvicornConfig) | A Uvicorn config whose bound socket can share its address with the workers' sockets.       |
| [`_ReusePortWorker`](#nemo_gym-server_runtime-uvicorn_reuse_port-_ReusePortWorker)               | A Uvicorn server that listens on its own SO_REUSEPORT socket instead of the inherited one. |

### Functions

| Name                                                                                             | Description                                                                                 |
| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------- |
| [`_bind_reuse_port_socket`](#nemo_gym-server_runtime-uvicorn_reuse_port-_bind_reuse_port_socket) | -                                                                                           |
| [`reuse_port_supported`](#nemo_gym-server_runtime-uvicorn_reuse_port-reuse_port_supported)       | SO_REUSEPORT load-balances connections across sockets on Linux only.                        |
| [`run_with_reuse_port`](#nemo_gym-server_runtime-uvicorn_reuse_port-run_with_reuse_port)         | `uvicorn.run` for a multi-worker server, with one SO_REUSEPORT listening socket per worker. |

### Data

[`LOG`](#nemo_gym-server_runtime-uvicorn_reuse_port-LOG)

### API

```python
class nemo_gym.server_runtime.uvicorn_reuse_port._ReusePortUvicornConfig()
```

**Bases:** `Config`

A Uvicorn config whose bound socket can share its address with the workers' sockets.

```python
nemo_gym.server_runtime.uvicorn_reuse_port._ReusePortUvicornConfig.bind_socket() -> socket.socket
```

```python
class nemo_gym.server_runtime.uvicorn_reuse_port._ReusePortWorker()
```

**Bases:** `Server`

A Uvicorn server that listens on its own SO_REUSEPORT socket instead of the inherited one.

```python
nemo_gym.server_runtime.uvicorn_reuse_port._ReusePortWorker.startup(
    sockets: typing.Optional[typing.List[socket.socket]] = None
) -> None
```

async

```python
nemo_gym.server_runtime.uvicorn_reuse_port._bind_reuse_port_socket(
    family: socket.AddressFamily,
    address: tuple,
    reuse_address: bool = True
) -> socket.socket
```

```python
nemo_gym.server_runtime.uvicorn_reuse_port.reuse_port_supported() -> bool
```

SO_REUSEPORT load-balances connections across sockets on Linux only.

```python
nemo_gym.server_runtime.uvicorn_reuse_port.run_with_reuse_port(
    uvicorn_kwargs: typing.Any = {}
) -> None
```

`uvicorn.run` for a multi-worker server, with one SO_REUSEPORT listening socket per worker.

Takes the same keyword arguments as `uvicorn.run`; `app` must be an import string and
`workers` above 1.

```python
nemo_gym.server_runtime.uvicorn_reuse_port.LOG = logging.getLogger('uvicorn.error')
```