Eve (Flask) 应用程序响应 uWSGI 没有内容

Eve (Flask) app responds with no content to uWSGI

我有一个非常简单的应用程序,其中有两个资源利用 Eve framework, which is in turn based on 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()

The built-in WSGI works just fine without *args, **kwargs on main().

uWSGI

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)

main() 方法被 uWSGI 命中,并传递两个对象(我记录到文件):

{'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字节。但是,我没有接触任何编码,并且我尝试了配置文件中的 httphttp-socket 指令,但没有进行任何更改。我怀疑,由于 uWSGI 将请求发送给 Eve,因此框架没有正确解析它。我查看了 running the included WSGI 的代码,但我似乎无法找到我需要重写以透明地解析请求的内容(如果有的话)。

  • uWSGI 在里面找不到你的 main main.py
  • 您需要 return 您的应用实例,然后将其传递到 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() 函数无关。

它只是在您的主文件中查找可调用实例。