Skip to content

Commit 02a1769

Browse files
committed
Add WEB_CONCURRENCY=auto support for automatic CPU detection
Allow --workers and WEB_CONCURRENCY to accept "auto" which resolves to os.cpu_count(), similar to Puma's approach. Useful for deployments on platforms that don't automatically set WEB_CONCURRENCY.
1 parent 7635258 commit 02a1769

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

plain/plain/cli/server.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import os
2+
13
import click
24

35
from plain.cli.runtime import without_runtime_setup
46

57

8+
def parse_workers(ctx: click.Context, param: click.Parameter, value: str) -> int:
9+
"""Parse workers value - accepts int or 'auto' for CPU count."""
10+
if value == "auto":
11+
return os.cpu_count() or 1
12+
return int(value)
13+
14+
615
@without_runtime_setup
716
@click.command()
817
@click.option(
@@ -22,10 +31,11 @@
2231
@click.option(
2332
"--workers",
2433
"-w",
25-
type=int,
26-
default=1,
34+
type=str,
35+
default="1",
2736
envvar="WEB_CONCURRENCY",
28-
help="Number of worker processes",
37+
callback=parse_workers,
38+
help="Number of worker processes (or 'auto' for CPU count)",
2939
show_default=True,
3040
)
3141
@click.option(

plain/plain/server/README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ By default, the server binds to `127.0.0.1:8000` and uses a single worker proces
2525
# Run with 4 worker processes
2626
plain server --workers 4
2727

28+
# Auto-detect based on available CPUs
29+
plain server --workers auto
30+
2831
# Run with 2 workers and 4 threads each
2932
plain server --workers 2 --threads 4
3033
```
@@ -62,7 +65,7 @@ All options are available via the command line. Run `plain server --help` to see
6265
| Option | Default | Description |
6366
| ------------------ | ---------------- | ----------------------------------------------------- |
6467
| `--bind` / `-b` | `127.0.0.1:8000` | Address to bind (can be used multiple times) |
65-
| `--workers` / `-w` | `1` | Number of worker processes |
68+
| `--workers` / `-w` | `1` | Number of worker processes (or `auto` for CPU count) |
6669
| `--threads` | `1` | Number of threads per worker |
6770
| `--timeout` / `-t` | `30` | Worker timeout in seconds |
6871
| `--reload` | `False` | Restart workers when code changes |
@@ -76,11 +79,11 @@ All options are available via the command line. Run `plain server --help` to see
7679

7780
## Environment variables
7881

79-
| Variable | Description |
80-
| --------------------- | -------------------------------------------------------------------- |
81-
| `WEB_CONCURRENCY` | Sets the number of workers (commonly used by Heroku and other PaaS) |
82-
| `SENDFILE` | Enable sendfile() syscall (`1`, `yes`, `true`, or `y` to enable) |
83-
| `FORWARDED_ALLOW_IPS` | Comma-separated list of trusted proxy IPs (default: `127.0.0.1,::1`) |
82+
| Variable | Description |
83+
| --------------------- | ------------------------------------------------------------------------ |
84+
| `WEB_CONCURRENCY` | Sets the number of workers (use `auto` to detect CPU cores, or a number) |
85+
| `SENDFILE` | Enable sendfile() syscall (`1`, `yes`, `true`, or `y` to enable) |
86+
| `FORWARDED_ALLOW_IPS` | Comma-separated list of trusted proxy IPs (default: `127.0.0.1,::1`) |
8487

8588
## Signals
8689

0 commit comments

Comments
 (0)