如何让uWSGI服务器在应用加载抛出异常时停止运行?
解决方案
在uwsgi.ini的[uwsgi]配置块中添加respawn-retry-limit = 0参数,修改后的配置如下:
[uwsgi] http = 0.0.0.0:9090 master = true processes = 1 gevent = 8 listen = 4096 buffer-size = 65535 uid = %u gid = %g ; emperor = 1 does not work too lazy = true lazy-app = false need-app = true module = wsgi_run die-on-term = true sharedarea = 1 ignore-signpipe = true harakiri-verbose = true memory-report = true log-master = true # 限制启动失败后的重试次数为0,直接退出服务 respawn-retry-limit = 0
原理说明
- 默认情况下,uWSGI会无限尝试重启启动失败的worker进程,导致陷入循环。
respawn-retry-limit = 0配置表示当worker加载应用失败时,不进行任何重试,直接终止整个uWSGI服务。- 配合已启用的
need-app = true,确保应用必须加载成功才能让worker正常运行,一旦加载失败立即触发终止逻辑。
若需要完全禁用所有worker的重启(包括运行中意外崩溃的情况),也可以使用no-respawn参数,但respawn-retry-limit更灵活,仅限制启动阶段的重试行为,更贴合当前场景需求。
内容的提问来源于stack exchange,提问作者rzlvmp
相关产品推荐
相关产品推荐

