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
When using `reload=True` or `workers=NUM`, you should put `uvicorn.run` into
21
+
an `if __name__ == '__main__'` clause in the main module.
22
+
23
+
3.**Environment Variables**: Use environment variables with the prefix `UVICORN_`.
24
+
```bash
25
+
export UVICORN_HOST="0.0.0.0"
26
+
export UVICORN_PORT="8000"
27
+
uvicorn main:app
28
+
```
15
29
16
-
Also note that `UVICORN_*` prefixed settings cannot be used from within an environment configuration file. Using an environment configuration file with the `--env-file` flag is intended for configuring the ASGI application that uvicorn runs, rather than configuring uvicorn itself.
30
+
CLI options and the arguments for `uvicorn.run()` take precedence over environment variables.
31
+
32
+
Also note that `UVICORN_*` prefixed settings cannot be used from within an environment
33
+
configuration file. Using an environment configuration file with the `--env-file` flag is
34
+
intended for configuring the ASGI application that uvicorn runs, rather than configuring
35
+
uvicorn itself.
17
36
18
37
## Application
19
38
20
39
*`APP` - The ASGI application to run, in the format `"<module>:<attribute>"`.
21
40
*`--factory` - Treat `APP` as an application factory, i.e. a `() -> <ASGI app>` callable.
41
+
*`--app-dir <path>` - Look for APP in the specified directory by adding it to the PYTHONPATH. **Default:***Current working directory*.
22
42
23
43
## Socket Binding
24
44
25
45
*`--host <str>` - Bind socket to this host. Use `--host 0.0.0.0` to make the application available on your local network. IPv6 addresses are supported, for example: `--host '::'`. **Default:***'127.0.0.1'*.
26
-
*`--port <int>` - Bind to a socket with this port. **Default:***8000*.
46
+
*`--port <int>` - Bind to a socket with this port. If set to 0, an available port will be picked. **Default:***8000*.
27
47
*`--uds <path>` - Bind to a UNIX domain socket, for example `--uds /tmp/uvicorn.sock`. Useful if you want to run Uvicorn behind a reverse proxy.
28
48
*`--fd <int>` - Bind to socket from this file descriptor. Useful if you want to run Uvicorn within a process manager.
29
49
30
50
## Development
31
51
32
-
*`--reload` - Enable auto-reload. Uvicorn supports two versions of auto-reloading behavior enabled by this option. There are important differences between them.
52
+
*`--reload` - Enable auto-reload. Uvicorn supports two versions of auto-reloading behavior enabled by this option. **Default:***False*.
33
53
*`--reload-dir <path>` - Specify which directories to watch for python file changes. May be used multiple times. If unused, then by default the whole current directory will be watched. If you are running programmatically use `reload_dirs=[]` and pass a list of strings.
54
+
*`--reload-delay <float>` - Delay between previous and next check if application needs to be reloaded. **Default:***0.25*.
34
55
35
56
### Reloading without watchfiles
36
57
@@ -40,7 +61,7 @@ If Uvicorn _cannot_ load [watchfiles](https://pypi.org/project/watchfiles/) at r
40
61
41
62
For more nuanced control over which file modifications trigger reloads, install `uvicorn[standard]`, which includes watchfiles as a dependency. Alternatively, install [watchfiles](https://pypi.org/project/watchfiles/) where Uvicorn can see it.
42
63
43
-
Using Uvicorn with watchfiles will enable the following options (which are otherwise ignored).
64
+
Using Uvicorn with watchfiles will enable the following options (which are otherwise ignored):
44
65
45
66
*`--reload-include <glob-pattern>` - Specify a glob pattern to match files or directories which will be watched. May be used multiple times. By default the following patterns are included: `*.py`. These defaults can be overwritten by including them in `--reload-exclude`.
46
67
*`--reload-exclude <glob-pattern>` - Specify a glob pattern to match files or directories which will excluded from watching. May be used multiple times. By default the following patterns are excluded: `.*, .py[cod], .sw.*, ~*`. These defaults can be overwritten by including them in `--reload-include`.
@@ -52,32 +73,36 @@ Using Uvicorn with watchfiles will enable the following options (which are other
52
73
53
74
## Production
54
75
55
-
*`--workers <int>` - Use multiple worker processes. Defaults to the `$WEB_CONCURRENCY` environment variable if available, or 1.
76
+
*`--workers <int>` - Number of worker processes. Defaults to the `$WEB_CONCURRENCY` environment variable if available, or 1. Not valid with `--reload`.
77
+
*`--env-file <path>` - Environment configuration file for the ASGI application. **Default:***None*.
78
+
79
+
!!! note
80
+
The `--reload` and `--workers` arguments are mutually exclusive. You cannot use both at the same time.
56
81
57
82
## Logging
58
83
59
84
*`--log-config <path>` - Logging configuration file. **Options:***`dictConfig()` formats: .json, .yaml*. Any other format will be processed with `fileConfig()`. Set the `formatters.default.use_colors` and `formatters.access.use_colors` values to override the auto-detected behavior.
60
85
* If you wish to use a YAML file for your logging config, you will need to include PyYAML as a dependency for your project or install uvicorn with the `[standard]` optional extras.
61
86
*`--log-level <str>` - Set the log level. **Options:***'critical', 'error', 'warning', 'info', 'debug', 'trace'.***Default:***'info'*.
62
87
*`--no-access-log` - Disable access log only, without changing log level.
63
-
*`--use-colors / --no-use-colors` - Enable / disable colorized formatting of the log records, in case this is not set it will be auto-detected. This option is ignored if the `--log-config` CLI option is used.
64
-
88
+
*`--use-colors / --no-use-colors` - Enable / disable colorized formatting of the log records. If not set, colors will be auto-detected. This option is ignored if the `--log-config` CLI option is used.
65
89
66
90
## Implementation
67
91
68
92
*`--loop <str>` - Set the event loop implementation. The uvloop implementation provides greater performance, but is not compatible with Windows or PyPy. **Options:***'auto', 'asyncio', 'uvloop'.***Default:***'auto'*.
69
93
*`--http <str>` - Set the HTTP protocol implementation. The httptools implementation provides greater performance, but it not compatible with PyPy. **Options:***'auto', 'h11', 'httptools'.***Default:***'auto'*.
70
94
*`--ws <str>` - Set the WebSockets protocol implementation. Either of the `websockets` and `wsproto` packages are supported. Use `'none'` to ignore all websocket requests. **Options:***'auto', 'none', 'websockets', 'wsproto'.***Default:***'auto'*.
71
-
*`--ws-max-size <int>` - Set the WebSockets max message size, in bytes. Please note that this can be used only with the default `websockets` protocol.
72
-
*`--ws-max-queue <int>` - Set the maximum length of the WebSocket incoming message queue. Please note that this can be used only with the default `websockets` protocol.
73
-
*`--ws-ping-interval <float>` - Set the WebSockets ping interval, in seconds. Please note that this can be used only with the default `websockets` protocol. **Default:***20.0*
74
-
*`--ws-ping-timeout <float>` - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default `websockets` protocol. **Default:***20.0*
95
+
*`--ws-max-size <int>` - Set the WebSockets max message size, in bytes. Only available with the `websockets` protocol. **Default:***16777216* (16 MB).
96
+
*`--ws-max-queue <int>` - Set the maximum length of the WebSocket incoming message queue. Only available with the `websockets` protocol. **Default:***32*.
97
+
*`--ws-ping-interval <float>` - Set the WebSockets ping interval, in seconds. Only available with the `websockets` protocol. **Default:***20.0*.
98
+
*`--ws-ping-timeout <float>` - Set the WebSockets ping timeout, in seconds. Only available with the `websockets` protocol. **Default:***20.0*.
99
+
*`--ws-per-message-deflate <bool>` - Enable/disable WebSocket per-message-deflate compression. Only available with the `websockets` protocol. **Default:***True*.
75
100
*`--lifespan <str>` - Set the Lifespan protocol implementation. **Options:***'auto', 'on', 'off'.***Default:***'auto'*.
76
-
*`--h11-max-incomplete-event-size <int>` - Set the maximum number of bytes to buffer of an incomplete event. Only available for `h11` HTTP protocol implementation. **Default:***'16384'* (16 KB).
101
+
*`--h11-max-incomplete-event-size <int>` - Set the maximum number of bytes to buffer of an incomplete event. Only available for `h11` HTTP protocol implementation. **Default:***16384* (16 KB).
77
102
78
103
## Application Interface
79
104
80
-
*`--interface` - Select ASGI3, ASGI2, or WSGI as the application interface.
105
+
*`--interface <str>` - Select ASGI3, ASGI2, or WSGI as the application interface.
81
106
Note that WSGI mode always disables WebSocket support, as it is not supported by the WSGI interface.
@@ -87,12 +112,12 @@ Note that WSGI mode always disables WebSocket support, as it is not supported by
87
112
88
113
## HTTP
89
114
90
-
*`--root-path <str>` - Set the ASGI `root_path` for applications submounted below a given URL path.
91
-
*`--proxy-headers` / `--no-proxy-headers` - Enable/Disable X-Forwarded-Proto, X-Forwarded-For to populate remote address info. Defaults to enabled, but is restricted to only trusting
92
-
connecting IPs in the `forwarded-allow-ips` configuration.
93
-
*`--forwarded-allow-ips` <comma-separated-list> Comma separated list of IP Addresses, IP Networks, or literals (e.g. UNIX Socket path) to trust with proxy headers. Defaults to the `$FORWARDED_ALLOW_IPS` environment variable if available, or '127.0.0.1'. The literal `'*'` means trust everything.
*`--root-path <str>` - Set the ASGI `root_path` for applications submounted below a given URL path.**Default:***""*.
116
+
*`--proxy-headers / --no-proxy-headers` - Enable/Disable X-Forwarded-Proto, X-Forwarded-For to populate remote address info. Defaults to enabled, but is restricted to only trusting connecting IPs in the `forwarded-allow-ips` configuration.
117
+
*`--forwarded-allow-ips <comma-separated-list>` - Comma separated list of IP Addresses, IP Networks, or literals (e.g. UNIX Socket path) to trust with proxy headers. Defaults to the `$FORWARDED_ALLOW_IPS` environment variable if available, or '127.0.0.1'. The literal `'*'` means trust everything.
*`--header <name:value>` - Specify custom default HTTP response headers as a Name:Value pair. May be used multiple times.
96
121
97
122
!!! note
98
123
The `--no-date-header` flag doesn't have effect on the `websockets` implementation.
@@ -104,18 +129,18 @@ The [SSL context](https://docs.python.org/3/library/ssl.html#ssl.SSLContext) can
104
129
*`--ssl-keyfile <path>` - The SSL key file.
105
130
*`--ssl-keyfile-password <str>` - The password to decrypt the ssl key.
106
131
*`--ssl-certfile <path>` - The SSL certificate file.
107
-
*`--ssl-version <int>` - The SSL version to use.
108
-
*`--ssl-cert-reqs <int>` - Whether client certificate is required.
132
+
*`--ssl-version <int>` - The SSL version to use.**Default:***ssl.PROTOCOL_TLS_SERVER*.
133
+
*`--ssl-cert-reqs <int>` - Whether client certificate is required.**Default:***ssl.CERT_NONE*.
109
134
*`--ssl-ca-certs <str>` - The CA certificates file.
110
-
*`--ssl-ciphers <str>` - The ciphers to use.
135
+
*`--ssl-ciphers <str>` - The ciphers to use.**Default:***"TLSv1"*.
111
136
112
137
To understand more about the SSL context options, please refer to the [Python documentation](https://docs.python.org/3/library/ssl.html).
113
138
114
139
## Resource Limits
115
140
116
141
*`--limit-concurrency <int>` - Maximum number of concurrent connections or tasks to allow, before issuing HTTP 503 responses. Useful for ensuring known memory usage patterns even under over-resourced loads.
117
142
*`--limit-max-requests <int>` - Maximum number of requests to service before terminating the process. Useful when running together with a process manager, for preventing memory leaks from impacting long-running processes.
118
-
*`--backlog <int>` - Maximum number of connections to hold in backlog. Relevant for heavy incoming traffic. **Default:***2048*
143
+
*`--backlog <int>` - Maximum number of connections to hold in backlog. Relevant for heavy incoming traffic. **Default:***2048*.
0 commit comments