Eventlet heroku flasksocketio 应用程序不工作

Posted

技术标签:

【中文标题】Eventlet heroku flasksocketio 应用程序不工作【英文标题】:Eventlet heroku flasksocketio app not working 【发布时间】:2021-05-26 17:06:56 【问题描述】:

我在将使用 websockets 的 Flask 应用程序部署到 heroku 时遇到了很多麻烦。它适用于heroku local,甚至在本地运行相同的命令时也能正常工作。

web:  gunicorn --worker-class eventlet --log-file=- app:app

该应用程序与此处的示例相同,只有很少的更改:https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/example/app.py

以下是错误日志:

2021-02-24T11:18:46.724927+00:00 heroku[web.1]: State changed from starting to up

2021-02-24T11:19:08.934471+00:00 app[web.1]: [2021-02-24 11:19:08 +0000] [4] [INFO] Shutting down: Master

2021-02-24T11:19:09.027108+00:00 heroku[web.1]: Process exited with status 0

2021-02-24T11:19:15.379018+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=break1n.herokuapp.com request_id=f1d4ceac-69a1-43fc-a08d-c7462c6501fd fwd="122.181.58.194" dyno=web.1 connect=1ms service=17217ms status=503 bytes=0 protocol=https

2021-02-24T11:19:15.374389+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [4] [CRITICAL] WORKER TIMEOUT (pid:8)

2021-02-24T11:19:15.374534+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [4] [CRITICAL] WORKER TIMEOUT (pid:9)

2021-02-24T11:19:15.377083+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [9] [ERROR] Socket error processing request.

2021-02-24T11:19:15.377084+00:00 app[web.1]: Traceback (most recent call last):

2021-02-24T11:19:15.377084+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 65, in handle

2021-02-24T11:19:15.377085+00:00 app[web.1]:     util.reraise(*sys.exc_info())

2021-02-24T11:19:15.377085+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 625, in reraise

2021-02-24T11:19:15.377086+00:00 app[web.1]:     raise value

2021-02-24T11:19:15.377086+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 38, in handle

2021-02-24T11:19:15.377086+00:00 app[web.1]:     listener_name = listener.getsockname()

2021-02-24T11:19:15.377087+00:00 app[web.1]: OSError: [Errno 9] Bad file descriptor

2021-02-24T11:19:15.377336+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [8] [ERROR] Socket error processing request.

2021-02-24T11:19:15.377336+00:00 app[web.1]: Traceback (most recent call last):

2021-02-24T11:19:15.377336+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 65, in handle

2021-02-24T11:19:15.377337+00:00 app[web.1]:     util.reraise(*sys.exc_info())

2021-02-24T11:19:15.377337+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 625, in reraise

2021-02-24T11:19:15.377337+00:00 app[web.1]:     raise value

2021-02-24T11:19:15.377338+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 38, in handle

2021-02-24T11:19:15.377338+00:00 app[web.1]:     listener_name = listener.getsockname()

2021-02-24T11:19:15.377338+00:00 app[web.1]: OSError: [Errno 9] Bad file descriptor

2021-02-24T11:19:15.377724+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [9] [INFO] Worker exiting (pid: 9)

2021-02-24T11:19:15.378140+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [8] [ERROR] Socket error processing request.

2021-02-24T11:19:15.378141+00:00 app[web.1]: Traceback (most recent call last):

2021-02-24T11:19:15.378141+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 65, in handle

2021-02-24T11:19:15.378142+00:00 app[web.1]:     util.reraise(*sys.exc_info())

2021-02-24T11:19:15.378142+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 625, in reraise

2021-02-24T11:19:15.378142+00:00 app[web.1]:     raise value

2021-02-24T11:19:15.378149+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 38, in handle

2021-02-24T11:19:15.378149+00:00 app[web.1]:     listener_name = listener.getsockname()

2021-02-24T11:19:15.378149+00:00 app[web.1]: OSError: [Errno 9] Bad file descriptor

2021-02-24T11:19:15.378747+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [8] [ERROR] Socket error processing request.

2021-02-24T11:19:15.378747+00:00 app[web.1]: Traceback (most recent call last):

2021-02-24T11:19:15.378748+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 65, in handle

2021-02-24T11:19:15.378748+00:00 app[web.1]:     util.reraise(*sys.exc_info())

2021-02-24T11:19:15.378748+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 625, in reraise

2021-02-24T11:19:15.378749+00:00 app[web.1]:     raise value

2021-02-24T11:19:15.378749+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base_async.py", line 38, in handle

2021-02-24T11:19:15.378749+00:00 app[web.1]:     listener_name = listener.getsockname()

2021-02-24T11:19:15.378749+00:00 app[web.1]: OSError: [Errno 9] Bad file descriptor

2021-02-24T11:19:15.379429+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [8] [INFO] Worker exiting (pid: 8)

2021-02-24T11:19:15.443250+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [10] [INFO] Booting worker with pid: 10

2021-02-24T11:19:15.504260+00:00 app[web.1]: [2021-02-24 11:19:15 +0000] [11] [INFO] Booting worker with pid: 11

2021-02-24T11:19:15.615154+00:00 app[web.1]: Staring Server........

2021-02-24T11:19:15.669596+00:00 app[web.1]: Staring Server........

更新:

我已经尝试了https://flask-socketio.readthedocs.io/en/latest/ 中的所有部署选项。 gevent 和 eventlet 似乎都不起作用

【问题讨论】:

尝试在没有 gunicorn 的情况下部署应用程序。成功后,尝试在本地运行 gunicorn。该应用可能与 gunicorn 不兼容。 让我试试 效果很好 【参考方案1】:

根据建议,这是 heroku 的问题,而不是 gunicorn 或 eventlet。修复该问题后,eventlet gunicorn 不合作。所以最后对于heroku,我切换到了uwsgi。 heroku 上的 ws 与 gunicorn 存在一些不兼容问题。

【讨论】:

【参考方案2】:

你试过了吗

web: gunicorn --worker-class eventlet -w 1 ...

当我没有设置-w 1 选项时,我经常会遇到错误。

【讨论】:

以上是关于Eventlet heroku flasksocketio 应用程序不工作的主要内容,如果未能解决你的问题,请参考以下文章

openstack中eventlet使用

raise RuntimeError('你需要使用 eventlet 服务器。'

使用 Eventlet 从 WSGI 应用程序获取客户端 IP 地址

Python eventlet

flask-socketio 在安装了 eventlet 的 aws 上不起作用

我无法安装 eventlet/gevent/greenlet