gevent monkey使用flask run进行修补在调试模式下失败

2024-10-02 22:25:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用grequests进行并发HTTP调用。为了能够在本地(我运行flask dev server的地方)测试它,我尝试使用monkey补丁gevent,在这里我面临一些问题

以下是我的目录结构:

project_root/
    myapp/
        __init__.py
        app.py
        wsgi.py
        somepackage/
            module_using_grequest.py
    

app.py模块包含用于创建flask应用程序的函数

def create_app():
    ...

wsgi.py模块初始化并存储app变量:

app = create_app()

因此,我像这样运行flask服务器:

FLASK_APP=myapp/wsgi.py flask run

当我在生产上运行时,我只是通过gunicorn运行它,并设置worker_class=gevent,它在内部处理monkey.patch_all()。但是对于DevelopmentServer,我试图找出一个地方来完成所有的补丁。我认为把它放在myapp/__init__.py会更好,因为在myapp/__init__.py文件中还有另一个导入。因此,无法将其放入myapp/wsgi.py文件中,因为__init__.py导入将在patch_all()之前运行

所以myapp/__init__.py变成这样:

from gevent import monkey

monkey.patch_all()

# other imports

我希望这能正确修补。但当我在非调试模式下运行flask server时,会收到以下警告:

grequests.py:22: MonkeyPatchWarning: Patching more than once will result in the union of all True parameters being patched

但是服务器运行良好,我可以使用grequests方法

但如果我在调试模式下运行此程序,则会收到以下警告:

FLASK_ENV=development FLASK_app=myapp/wsgi.py flask run

__init__.py:5: MonkeyPatchWarning: Monkey-patching outside the main native thread. Some APIs will not be available. Expect a KeyError to be printed at shutdown.
  monkey.patch_all()
__init__.py:5: MonkeyPatchWarning: Monkey-patching not on the main thread; threading.main_thread().join() will hang from a greenlet
  monkey.patch_all()

它就挂在那里。服务器没有运行。我做错什么了吗?这个版本是gevent==21.1.2版本

早些时候,当我在gevent==1.2.1上运行相同的东西时,它输出了以下错误:

/Users/rohitjain/sourcecode/project_root/__init__.py:5: RuntimeWarning: Monkey-patching not on the main thread; threading.main_thread().join() will hang from a greenlet
  monkey.patch_all()
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/rohitjain/.venvs/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 963, in inner
    fd=fd,
  File "/Users/rohitjain/.venvs/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 806, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "/Users/rohitjain/.venvs/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 694, in __init__
    server_address = get_sockaddr(host, int(port), self.address_family)
  File "/Users/rohitjain/.venvs/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 660, in get_sockaddr
    host, port, family, socket.SOCK_STREAM, socket.IPPROTO_TCP
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
  File "/Users/rohitjain/.venvs/venv/bin/../lib/python3.6/encodings/__init__.py", line 100, in search_function
    level=0)
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 152, in __exit__
  File "<frozen importlib._bootstrap>", line 107, in release
RuntimeError: cannot release un-acquired lock

服务器就挂在那里


Tags: inpyappflaskinitliblineall