You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法使用Socket.io、Gunicorn和eventlet部署极简Flask应用

使用Gunicorn+Eventlet部署Flask-Socket.IO应用时遇到的问题及解决

我尝试用Gunicorn搭配Eventlet部署Flask-Socket.IO应用,执行启动命令gunicorn --worker-class eventlet -w 1 myapp:app时,进程反复抛出以下错误直至终止:

[2023-08-22 19:51:01 -0400] [57647] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
    worker.init_process()
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/gunicorn/workers/geventlet.py", line 143, in init_process
    super().init_process()
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/gunicorn/workers/base.py", line 142, in init_process
    self.run()
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/gunicorn/workers/geventlet.py", line 173, in run
    eventlet.sleep(1.0)
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/eventlet/greenthread.py", line 36, in sleep
    hub.switch()
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/eventlet/hubs/hub.py", line 313, in switch
    return self.greenlet.switch()
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/eventlet/hubs/hub.py", line 365, in run
    self.wait(sleep_time)
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/eventlet/hubs/kqueue.py", line 98, in wait
    result = self._control([], self.MAX_EVENTS, seconds)
  File "/Users/alexpalacios/Documents/Projects/spotify-game/venv/lib/python3.8/site-packages/eventlet/hubs/kqueue.py", line 39, in _control
    return self.kqueue.control(events, max_events, timeout)
OSError: [Errno 9] Bad file descriptor

代码与依赖版本

应用代码(myapp.py)

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@socketio.on('message')
def handle_message(data):
    print('received message: ' + data)

if __name__ == '__main__':
    socketio.run(app)

核心依赖版本

Python==3.8.8
Flask==2.3.2
Flask-SocketIO==5.3.5
eventlet==0.33.3
gunicorn==21.2.0

用python myapp.py可以正常启动应用,但我需要在Render.com部署正式环境,必须让Gunicorn正常工作。

其他尝试的解决操作

我在代码中添加了eventlet.monkey_patch(),然后执行命令gunicorn -w 1 myapp:app,服务器能启动,也能通过Postman连接,但连接会在短时间内中断,同时收到以下错误:

[2023-08-22 20:10:52 -0400] [57992] [CRITICAL] WORKER TIMEOUT (pid:57994)
[2023-08-22 20:10:52 -0400] [57994] [INFO] Worker exiting (pid: 57994)
[2023-08-22 20:10:53 -0400] [57992] [ERROR] Worker (pid:57994) was sent SIGKILL! Perhaps out of memory?
[2023-08-22 20:10:53 -0400] [58006] [INFO] Booting worker with pid: 58006

最终解决方法

我通过删除虚拟环境并重新安装依赖解决了该问题,以下是新旧依赖清单对比:

旧requirements.txt

annotated-types==0.5.0
anyio==3.7.1
async-timeout==4.0.2
bidict==0.22.1
blinker==1.6.2
certifi==2023.5.7
charset-normalizer==3.2.0
click==8.1.6
cycler==0.11.0
dnspython==2.4.2
eventlet==0.33.3
exceptiongroup==1.1.2
Flask==2.3.2
Flask-Cors==4.0.0
Flask-SocketIO==5.3.5
fonttools==4.40.0
greenlet==2.0.2
gunicorn==21.2.0
h11==0.14.0
httpcore==0.17.3
idna==3.4
importlib-metadata==6.8.0
importlib-resources==6.0.0
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
kiwisolver==1.4.4
MarkupSafe==2.1.3
marshmallow==3.20.1
matplotlib==3.7.2
numpy==1.24.4
packaging==23.1
Pillow==10.0.0
pluggy==1.2.0
pydantic-core==2.4.0
pymongo==4.4.1
pyparsing==3.0.9
pytest==7.4.0
python-dateutil==2.8.2
python-dotenv==1.0.0
python-engineio==4.5.1
python-socketio==5.8.0
python-vlc==3.0.18122
redis==4.6.0
requests==2.31.0
six==1.16.0
sniffio==1.3.0
spotipy==2.23.0
tomli==2.0.1
typing-extensions==4.7.1
urllib3==2.0.4
Werkzeug==2.3.6
zipp==3.15.0

新requirements.txt

async-timeout==4.0.3
bidict==0.22.1
blinker==1.6.2
certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.7
dnspython==2.4.2
eventlet==0.33.3
flask==2.3.3
Flask-Cors==4.0.0
Flask-SocketIO==5.3.5
greenlet==2.0.2
gunicorn==21.2.0
idna==3.4
importlib-metadata==6.8.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
marshmallow==3.20.1
packaging==23.1
pymongo==4.5.0
python-dotenv==1.0.0
python-engineio==4.6.1
python-socketio==5.8.0
redis==5.0.0
requests==2.31.0
six==1.16.0
spotipy==2.23.0
urllib3==2.0.4
werkzeug==2.3.7
zipp==3.16.2

内容的提问来源于stack exchange,提问作者alexpalaciosjr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 21:54:59