Why gevent on a Flask app with Apache + mod_wsgi is raising NotImplementedError?

非 Y 不嫁゛ 提交于 2020-01-03 17:32:04

问题


I'm having an issue deploying my Flask app with Apache (mod_wsgi) and gevent on a shared hosting (Webfaction).

The application works fine in the development server provided by Flask, but when I try to deploy it I get the following error in 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'm using the python-requests' async module to make concurrent HTTP requests. I tried to Google around but the only advice I found is to to call

from gevent import monkey
monkey.patch_all()

something that I already do in my code.

The value of WSGIDaemonProcess is:

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

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

Anybody has any advice to solve this issue?


回答1:


It seems like i found the solution myself. The following directive solved my issue:

WSGIApplicationGroup %{GLOBAL}

The idea comes from another answer where it is suggested to set WSGIApplicationGroup to GLOBAL to solve a problem with a WSGI process that keep crashing. From WSGI documentation:

To force a specific WSGI application to be run within the very first Python sub interpreter created when Python is initialised, the WSGIApplicationGroup directive should be used and the group set to '%{GLOBAL}'.

Cannot fully understand why this directive solve my issue but it does. I will be more than happy if someone is able to explain this to me in plain English ;-)




回答2:


Try replacing monkey.patch_all() with monkey.patch_all(thread=False). If it's really the threading module which is causing the trouble when patched, this should solve it. request does not use threading.




回答3:


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

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

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

Note that Flask can run standalone 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 want an HTTP server in front of it such as Gunicorn.



来源:https://stackoverflow.com/questions/9692089/why-gevent-on-a-flask-app-with-apache-mod-wsgi-is-raising-notimplementederror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!