You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove SyncWorker, make ThreadWorker the only worker type
ThreadWorker with threads=1 behaves nearly identically to SyncWorker,
so there's no need for two worker implementations. This also updates
the CLI defaults to --workers auto --threads 4 and validates that
threads >= 1.
Copy file name to clipboardExpand all lines: plain/plain/server/README.md
+20-27Lines changed: 20 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
**A production-ready WSGI HTTP server based on gunicorn.**
4
4
5
5
-[Overview](#overview)
6
-
-[Worker types](#worker-types)
6
+
-[Workers and threads](#workers-and-threads)
7
7
-[Configuration options](#configuration-options)
8
8
-[Environment variables](#environment-variables)
9
9
-[Signals](#signals)
@@ -19,44 +19,37 @@ You can run the built-in HTTP server with the `plain server` command.
19
19
plain server
20
20
```
21
21
22
-
By default, the server binds to `127.0.0.1:8000` and uses a single worker process. In production, you will typically want to increase the number of workers and optionally enable threading.
23
-
24
-
```bash
25
-
# Run with 4 worker processes
26
-
plain server --workers 4
27
-
28
-
# Auto-detect based on available CPUs
29
-
plain server --workers auto
30
-
31
-
# Run with 2 workers and 4 threads each
32
-
plain server --workers 2 --threads 4
33
-
```
22
+
By default, the server binds to `127.0.0.1:8000` with one worker process per CPU core and 4 threads per worker.
34
23
35
24
For local development, you can enable auto-reload to restart workers when code changes.
36
25
37
26
```bash
38
27
plain server --reload
39
28
```
40
29
41
-
## Worker types
30
+
## Workers and threads
42
31
43
-
The server automatically selects the worker type based on your configuration.
32
+
The server uses two levels of concurrency:
44
33
45
-
**Sync workers** handle one request at a time per worker. These are simple and predictable.
34
+
-**Workers** are separate OS processes. Each worker runs independently with its own memory. The default is `auto`, which spawns one worker per CPU core.
35
+
-**Threads** run inside each worker. Threads share memory within a worker and handle concurrent requests using a thread pool. The default is 4 threads per worker.
46
36
47
-
```bash
48
-
# Single-threaded (uses sync worker)
49
-
plain server --workers 4
50
-
```
37
+
Total concurrent requests = `workers × threads`. On a 4-core machine with the defaults, that's `4 × 4 = 16` concurrent requests.
38
+
39
+
**When to adjust workers:** Workers provide true parallelism since each is a separate process with its own Python GIL. More workers means more memory usage but better CPU utilization. Use `--workers auto` (the default) to match your CPU cores, or set an explicit number.
51
40
52
-
**Threaded workers**handle multiple concurrent requests per worker using a thread pool. These are useful when your application does blocking I/O.
41
+
**When to adjust threads:**Threads are efficient for I/O-bound work (database queries, external API calls) since they release the GIL while waiting. Most web applications are I/O-bound, so the default of 4 threads works well. Increase threads if your application spends a lot of time waiting on I/O. Decrease to 1 if you need to avoid thread-safety concerns.
53
42
54
43
```bash
55
-
# Multi-threaded (uses threaded worker)
56
-
plain server --workers 2 --threads 8
57
-
```
44
+
# Explicit worker count
45
+
plain server --workers 2
58
46
59
-
For advanced worker customization, see the [`SyncWorker`](./workers/sync.py#SyncWorker) and [`ThreadWorker`](./workers/thread.py#ThreadWorker) classes.
47
+
# More threads for I/O-heavy apps
48
+
plain server --threads 8
49
+
50
+
# Single-threaded workers (simplest, one request at a time per worker)
51
+
plain server --threads 1
52
+
```
60
53
61
54
## Configuration options
62
55
@@ -65,8 +58,8 @@ All options are available via the command line. Run `plain server --help` to see
0 commit comments