简体   繁体   中英

Eve (Flask) app responds with no content to uWSGI

I have very simple application with two resources leveraging the Eve framework, which is in turn based on Flask .

TL;DR:

The built in WSGI works fine, but uWSGI is not receiving content from the script. Am I configuring the WSGI correctly to communicate with Eve, or do I need to modify the default Flask configuration?

Eve

I manage everything (at the moment) through a single main.py which looks like this:

## 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

The configuration for uWSGI has been taken from here with modification only for a virtual environment, log, and module and callable:

[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

I have disabled and enabled threading with no success in either direction.

Logs and Responses

From the uWSGI logs:

*** 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)

The main() method is being hit by uWSGI, and is passing two objects (which I logged to file):

{'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>

Notes

This seems like it is a mismatch of encoding in the response, hence the 0 byte response in the uWSGI logs. However, I'm not touching any of encoding, and I tried both the http and http-socket directives in the config file without any changes. I have a suspicion that, since uWSGI is sending the request on to Eve, the framework isn't parsing it correctly. I've looked through the code for running the included WSGI , and I can't seem to find what, if anything, I need to override to parse the request transparently.

  • uWSGI can not find your main inside main.py
  • you need to return an instance of your app and then pass it in uwsgi ini file. like this.

    in main.py:

     def main(): app = .... // whatever you want return app app = main()

then in uwsgi file:

[uwsgi]
http = 127.0.0.1:5000
module = main
callable = app

default wsgi in flask works because it looks inside serve() function in which you have made an instance of your app.

but uwsgi has nothing to do with that serve() function.

it just looks for callable instance in your main file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM