Why does a gevent in a Flask application with Apache + mod_wsgi raise a NotImplementedError?

I had a problem deploying my Flask application with Apache (mod_wsgi) and gevent on shared hosting (Webfaction).

The application works fine on the development server provided by Flask, but when I try to deploy it, I get the following error in the log file:

[Tue Mar 13 15:48:24 2012] [error] Traceback (most recent call last): [Tue Mar 13 15:48:24 2012] [error] File "evdns.pxi", line 78, in gevent.core.__evdns_callback (gevent/core.c:6300) [Tue Mar 13 15:48:24 2012] [error] File "/home/username/.virtualenvs/staging/lib/python2.7/site-packages/gevent/hub.py", line 297, in switch_args [Tue Mar 13 15:48:24 2012] [error] File "/home/username/.virtualenvs/staging/lib/python2.7/site-packages/gevent/hub.py", line 290, in switch [Tue Mar 13 15:48:24 2012] [error] File "/home/username/.virtualenvs/staging/lib/python2.7/site-packages/gevent/hub.py", line 135, in get_hub [Tue Mar 13 15:48:24 2012] [error] NotImplementedError: gevent is only usable from a single thread 

I need gevent because I use the asynchronous python request module to create concurrent HTTP requests. I tried using Google, but the only advice I found was to call

 from gevent import monkey monkey.patch_all() 

what I'm already doing in my code.

WSGIDaemonProcess value:

 WSGIDaemonProcess myapp processes=5 python-path=/home/myusername/webapps/myapp/lib/python2.7 threads=1 

Here is my httpd.conf: http://pastebin.com/eWygicJH

Anyone have any recommendations on how to solve this problem?

+4
source share
3 answers

I seem to have found a solution myself. The following directive solved my problem:

 WSGIApplicationGroup %{GLOBAL} 

The idea comes from another answer, which suggests setting WSGIApplicationGroup to GLOBAL, to solve the problem with the WSGI process that continues to fail. From the WSGI documentation :

To force a particular WSGI application to run in the very first Python Sub-interpreter created during Python initialization, use the WSGIApplicationGroup directive and the group '% {GLOBAL}'.

I can’t understand why this directive solves my problem, but it does. I will be more than happy if someone can explain this to me in simple terms; -)

+2
source

Try replacing monkey.patch_all() with monkey.patch_all(thread=False) . If this is really a streaming module that causes a problem during the fix, this should fix the problem. request does not use streams.

+1
source

I posted below answer at https://serverfault.com/a/869625/355861

apache mod_wsgi is currently not compatible with gevent. For AWS elastic bean stitch with Apache, I used async_mode = "threading" for Flask, and it works well. Note that threads have lower performance than gevent. https://flask-socketio.readthedocs.io/en/latest/#deployment

 app = Flask(__name__,static_folder='static') socketio = SocketIO(app, async_mode="threading") 

Please note that the checkbox can work autonomously with gevent.

 app = Flask(__name__,static_folder='static') socketio = SocketIO(app, async_mode="gevent") if __name__ == '__main__': HOST = '127.0.0.1' PORT = 5055 socketio.run(app, port=PORT, host=HOST) 

However, you really need an HTTP server in front of it, such as Gunicorn.

0
source

Source: https://habr.com/ru/post/1401249/


All Articles