Windows上Flask项目生产环境部署

最近写了一个公司项目,开发环境和生产环境都是使用的Windows操作系统,开发测试阶段很快就完成了,但是在部署阶段遇到了些问题,下面记录解决过程。

存在问题

之间部署项目都是在Linux上进行部署,采用gunicorn+nginx进行部署,本来还是采用这两个中间件进行部署。但是,查找资料后发现gunicorn是基于unix系统开发的,并不兼容windows操作系统,主要这个问题的解决,在Windows上使用什么应用服务器代替gunicorn.

解决问题

一顿搜索之后发现,waitress可以解决这个问题,一顿研究后,发现命令也挺简单,操作和gunicorn差不多,现在记录下具体操作过程。

安装waitress

1
pip install waitress

安装后,会发现有个waitress-server.exe的程序:

将其添加到环境变量中,即可完成安装。

使用waitress

使用起来也非常方便,类似gunicorn差不多,下面给我使用的命令

1
waitress-server.exe --port=5000 --threads=10 app:app

具体参数还有很多,如下:

1
2
--help
3
Show this information.
4
--call
5
Call the given object to get the WSGI application.
6
--host=ADDR
7
Hostname or IP address on which to listen, default is '0.0.0.0', which means "all IP addresses on this host".
8
--port=PORT
9
TCP port on which to listen, default is '8080'
10
--listen=host:port
11
Tell waitress to listen on an ip port combination.
12
13
Example:
14
15
--listen=127.0.0.1:8080 --listen=[::1]:8080 --listen=*:8080
16
This option may be used multiple times to listen on multipe sockets. A wildcard for the hostname is also supported and will bind to both IPv4/IPv6 depending on whether they are enabled or disabled.
17
18
--[no-]ipv4
19
Toggle on/off IPv4 support.
20
21
This affects wildcard matching when listening on a wildcard address/port combination.
22
23
--[no-]ipv6
24
Toggle on/off IPv6 support.
25
26
This affects wildcard matching when listening on a wildcard address/port combination.
27
28
--unix-socket=PATH
29
Path of Unix socket. If a socket path is specified, a Unix domain socket is made instead of the usual inet domain socket.
30
31
Not available on Windows.
32
33
--unix-socket-perms=PERMS
34
Octal permissions to use for the Unix domain socket, default is '600'.
35
--url-scheme=STR
36
Default wsgi.url_scheme value, default is 'http'.
37
--url-prefix=STR
38
The SCRIPT_NAME WSGI environment value. Setting this to anything except the empty string will cause the WSGI SCRIPT_NAME value to be the value passed minus any trailing slashes you add, and it will cause the PATH_INFO of any request which is prefixed with this value to be stripped of the prefix. Default is the empty string.
39
--ident=STR
40
Server identity used in the 'Server' header in responses. Default is 'waitress'.
41
Tuning options:
42
43
--threads=INT
44
Number of threads used to process application logic, default is 4.
45
--backlog=INT
46
Connection backlog for the server. Default is 1024.
47
--recv-bytes=INT
48
Number of bytes to request when calling socket.recv(). Default is 8192.
49
--send-bytes=INT
50
Number of bytes to send to socket.send(). Default is 1. Multiples of 9000 should avoid partly-filled TCP packets.
51
52
Deprecated since version 1.3.
53
54
--outbuf-overflow=INT
55
A temporary file should be created if the pending output is larger than this. Default is 1048576 (1MB).
56
--outbuf-high-watermark=INT
57
The app_iter will pause when pending output is larger than this value and will resume once enough data is written to the socket to fall below this threshold. Default is 16777216 (16MB).
58
--inbuf-overflow=INT
59
A temporary file should be created if the pending input is larger than this. Default is 524288 (512KB).
60
--connection-limit=INT
61
Stop creating new channels if too many are already active. Default is 100.
62
--cleanup-interval=INT
63
Minimum seconds between cleaning up inactive channels. Default is 30. See --channel-timeout.
64
--channel-timeout=INT
65
Maximum number of seconds to leave inactive connections open. Default is 120. 'Inactive' is defined as 'has received no data from the client and has sent no data to the client'.
66
--[no-]log-socket-errors
67
Toggle whether premature client disconnect tracebacks ought to be logged. On by default.
68
--max-request-header-size=INT
69
Maximum size of all request headers combined. Default is 262144 (256KB).
70
--max-request-body-size=INT
71
Maximum size of request body. Default is 1073741824 (1GB).
72
--[no-]expose-tracebacks
73
Toggle whether to expose tracebacks of unhandled exceptions to the client. Off by default.
74
--asyncore-loop-timeout=INT
75
The timeout value in seconds passed to asyncore.loop(). Default is 1.
76
--asyncore-use-poll
77
The use_poll argument passed to asyncore.loop(). Helps overcome open file descriptors limit. Default is False.