我有一个非常简单的应用程序,它利用了Eve框架,而这两个资源又是基于Flask的。
内置的WSGI可以正常工作,但是uWSGI不能从脚本接收内容。我是否已正确配置WSGI以与Eve进行通信,还是需要修改默认的Flask配置?
我现在通过一个main.py
来管理一切(如下所示):
## main.py
from eve import Eve
from settings import these_settings
def post_post_callback():
...
def post_get_callback():
...
# def main():
# `-> throws TypeError, main() expects exactly 0 arguments
def main(*args, **kwargs):
app = Eve(settings=these_settings)
app.on_post_POST += post_post_callback
app.on_post_GET += post_get_callback
return app
def serve():
app = main()
app.run()
内置的WSGI在
*args, **kwargs
上没有main()
的情况下也可以正常工作。
uWSGI的配置取自here,仅针对虚拟环境,日志和模块且可调用进行了修改:
[uwsgi]
http = 127.0.0.1:5000
module = main
callable = main
virtualenv = /Users/me/.local/share/virtualenvs/my_app-qtX0KE_c
processes = 4
enable-threads = true
threads = 2
logto = uwsgi.log
我已禁用和启用线程,但在任一方向上均未成功。
从uWSGI日志中:
*** Starting uWSGI 2.0.17.1 (64bit) on [Fri Sep 28 15:29:28 2018] ***
compiled with version: 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2) on 27 September 2018 19:43:49
os: Darwin-17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64
nodename: GVOMB0036.local
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /Users/me/my_app
detected binary path: /usr/local/bin/uwsgi
your processes number limit is 1418
your memory page size is 4096 bytes
detected max file descriptor number: 256
lock engine: OSX spinlocks
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 127.0.0.1:5000 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:51246 (port auto-assigned) fd 3
Python version: 2.7.15 (default, Sep 18 2018, 20:16:18) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Set PythonHome to /Users/me/.local/share/virtualenvs/my_app-qtX0KE_c
Python main interpreter initialized at 0x7f9966d01280
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 416560 bytes (406 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7f9966d01280 pid: 23625 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 23625)
spawned uWSGI worker 1 (pid: 23628, cores: 2)
spawned uWSGI worker 2 (pid: 23629, cores: 2)
spawned uWSGI worker 3 (pid: 23630, cores: 2)
spawned uWSGI worker 4 (pid: 23631, cores: 2)
spawned uWSGI http 1 (pid: 23632)
[pid: 23630|app: 0|req: 1/1] 127.0.0.1 () {36 vars in 460 bytes} [Fri Sep 28 15:29:39 2018] GET / => generated 0 bytes in 9 msecs (HTTP/1.1 500) 0 headers in 0 bytes (2 switches on core 0)
uWSGI击中了main()
方法,并传递了两个对象(我记录到文件中):
{'wsgi.multiprocess': True, 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'UWSGI_ROUTER': 'http', 'SERVER_PROTOCOL': 'HTTP/1.1', 'QUERY_STRING': '', 'HTTP_USER_AGENT': 'PostmanRuntime/7.3.0', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': 'GVOMB0036.local', 'REMOTE_ADDR': '127.0.0.1', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '5000', 'uwsgi.node': 'GVOMB0036.local', 'HTTP_POSTMAN_TOKEN': '12bd37b7-7d07-4701-a59b-54a5e3ee108d', 'uwsgi.core': 0, 'wsgi.input': <uwsgi._Input object at 0x100f674b0>, 'HTTP_HOST': '127.0.0.1:5000', 'wsgi.multithread': True, 'HTTP_CACHE_CONTROL': 'no-cache', 'REQUEST_URI': '/schemata', 'HTTP_ACCEPT': '*/*', 'wsgi.version': (1, 0), 'wsgi.run_once': False, 'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0x101c41f60>, 'REMOTE_PORT': '32486', 'uwsgi.version': '2.0.17.1', 'wsgi.file_wrapper': <built-in function uwsgi_sendfile>, 'HTTP_ACCEPT_ENCODING': 'gzip, deflate', 'PATH_INFO': '/'}
<built-in function uwsgi_spit>
此似乎就像响应中的编码不匹配一样,因此uWSGI日志中的0字节响应。但是,我没有涉及任何编码,我在配置文件中尝试了http
和http-socket
伪指令,没有进行任何更改。我怀疑,由于uWSGI将请求发送给Eve,因此框架无法正确解析它。我已经浏览了running the included WSGI的代码,但似乎找不到什么(如果有的话)需要重写以透明地解析请求。
答案 0 :(得分:0)
,您需要返回应用程序的实例,然后将其传递给uwsgi ini文件。这样。
在main.py中:
def main():
app = ....
// whatever you want
return app
app = main()
然后在uwsgi文件中:
[uwsgi]
http = 127.0.0.1:5000
module = main
callable = app
flask中的默认wsgi可以工作,因为它在您创建了应用程序实例的serve()函数内部。
但是uwsgi与serve()函数无关。
它只是在您的主文件中查找可调用实例。