微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用烧瓶运行修补 gevent 猴子在调试模式下失败

如何解决使用烧瓶运行修补 gevent 猴子在调试模式下失败

我试图使用 grequests 进行并发 HTTP 调用。为了能够在我运行 Flask 开发服务器的本地测试它,我试图修补 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()。但是对于开发服务器,我试图找出做 patch_all 的地方。我认为将它放在 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 服务器时,我收到了这个警告:

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

服务器挂在那里。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。