Open buxizhizhoum opened 2 years ago
create a file called geventlet-config.py
with the following content
try:
import gevent.monkey
gevent.monkey.patch_all()
except ImportError:
pass
now add the following argument to your gunicorn. --config geventlet-config.py
So if you started gunicorn
using gunicorn --config geventlet-config.py -w 8 --worker-class gevent -b 0.0.0.0:5000 app:app
. The patch should be applied
delete --preload
create a file called
geventlet-config.py
with the following contenttry: import gevent.monkey gevent.monkey.patch_all() except ImportError: pass
now add the following argument to your gunicorn.
--config geventlet-config.py
So if you started
gunicorn
usinggunicorn --config geventlet-config.py -w 8 --worker-class gevent -b 0.0.0.0:5000 app:app
. The patch should be applied
Maybe I'm missing something? This solution doesn't work for me - I still get the same message about how "Monkey-patching ssl after ssl has already been imported may lead to errors" even when I do the monkey patch in the gunicorn config file. And when I look into gunicorn's code that processes config, it imports ssl, which makes me wonder how this could ever work.
python 3.10.6 Flask==3.0.2 gevent==24.2.1 gunicorn==22.0.0
Recently we encountered a problem, if we use
requests
in aflask
project, it will finally trigger an warning:/Users/buxizhizhoum/.virtualenvs/gunicorn_monkey_patch/lib/python3.6/site-packages/gunicorn/workers/ggevent.py:38: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016. Modules that had direct imports (NOT patched): ['urllib3.util (/Users/bytedance/.virtualenvs/gunicorn_monkey_patch/lib/python3.6/site-packages/urllib3/util/__init__.py)', 'urllib3.util.ssl_ (/Users/bytedance/.virtualenvs/gunicorn_monkey_patch/lib/python3.6/site-packages/urllib3/util/ssl_.py)'].
This could be reproduced with the code below: the app.py filethe command to start it:
gunicorn -w 8 --worker-class gevent --preload -b 0.0.0.0:5000 app:app
A simplified start procedure of gunicorn is: 1.parse config 2.load app 3.init worker gunicorn imported the
ssl
module(config.py
in gevent) when loading config,however the monkey patch is called when init the worker which is definitely after import ofssl
, this is why we get the warning that mentioned above.We tired 3 ways to solve this problem
app.py
GeventWorker.patch
ingevent.ggevent.py
, changemonkey.patch_all()
tomonkey.patch_all(ssl=False)
the method 1 has no use, since import
ssl
is before load app the method 2 could work, if we regardless of the drawback that we patched 2 times, method 2 is almost the best work around that I could find. the method 3 could also work, the drawback isssl
is blocking now.And besides all of above, I just wonder could we provide a choice that allow users to set
ssl
patch to False, and it could solve the problem, considering the fact that patch ssl after import may cause severe problems, leavessl
not patched might be an solution for someone that could accept the performance lose?And if wen change
app.py
file to lines below, it could triggerRecursionError: maximum recursion depth exceeded while calling a Python object
app.py that could trigger RecursionErrorWhen we start it with gunicorn and call http://127.0.0.1/test it will raise
RecursionError
, which is mentioned at Gunicorn RecursionError with gevent and requests in python 3.6.2The environment