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

Gunicorn + eventlet 在 SIGTERM 信号处理程序中使用 redis 连接

如何解决Gunicorn + eventlet 在 SIGTERM 信号处理程序中使用 redis 连接

我遇到了与使用 gunicorn eventlet 服务器在 SigTerm 处理程序中使用活动 I/O 连接相关的问题。

server.py

def exit_with_grace(*args):
    conn = get_redis_connection()
    conn.set('exited_gracefully',True)

signal.signal(signal.SIGTERM,exit_with_grace)

我也尝试启动 celery 任务(使用 amqp 代理),但我所有的想法都失败了。当我使用 python server.py 在调试模式下启动服务器时,它运行良好。 Gunicorn+ eventlet 不允许在 sigterm 处理程序中连接到 redis,导致出现以下错误

Traceback (most recent call last):
  File "/project/handlers/socketio/redis_context_backend.py",line 256,in publish_pattern
    return conn.publish(pattern,serialized)
  File "/project/venv/lib/python3.6/site-packages/redis/client.py",line 3098,in publish
    return self.execute_command('PUBLISH',channel,message)
  File "/project/venv/lib/python3.6/site-packages/redis/client.py",line 898,in execute_command
    conn = self.connection or pool.get_connection(command_name,**options)
  File "/project/venv/lib/python3.6/site-packages/redis/connection.py",line 1192,in get_connection
    connection.connect()
  File "/project/venv/lib/python3.6/site-packages/redis/connection.py",line 559,in connect
    sock = self._connect()
  File "/project/venv/lib/python3.6/site-packages/redis/connection.py",line 603,in _connect
    sock.connect(socket_address)
  File "/project/venv/lib/python3.6/site-packages/eventlet/greenio/base.py",line 250,in connect
    self._trampoline(fd,write=True)
  File "/project/venv/lib/python3.6/site-packages/eventlet/greenio/base.py",line 210,in _trampoline
    mark_as_closed=self._mark_as_closed)
  File "/project/venv/lib/python3.6/site-packages/eventlet/hubs/__init__.py",line 142,in trampoline
    assert hub.greenlet is not current,'do not call blocking functions from the mainloop'

Gunicorn 命令:

gunicorn --worker-class eventlet -w 1 server:ws --reload -b localhost:5001

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