使用supervisor后台运行celery

会有一股神秘感。 提交于 2019-12-11 13:02:37

使用supervisor后台运行celery

1. 安装supervisor

pip install supervisor

2. 配置supervisor

2.1 生成默认配置文件

# 生成的配置文件可指定路径
echo_supervisord_conf > /etc/supervisord.conf

2.2 修改配置文件

最后一行添加

[program:celery.worker] 
;指定运行目录 
directory=/root/my_prj
;运行目录下执行命令,日志保存在项目目录下
command=celery -A my_prj worker --loglevel info --logfile pro_celery_worker.log

;启动设置 
numprocs=1          ;进程数
autostart=true      ;当supervisor启动时,程序将会自动启动 
autorestart=true    ;自动重启

;停止信号,默认TERM 
stopsignal=INT

如需启动多个,则新增一个块,比如下面

[program:pro.celery.worker] 
;指定运行目录 
directory=/root/my_prj1
;运行目录下执行命令,日志保存在项目目录下
command=celery -A my_prj1 worker --loglevel info --logfile pro_celery_worker.log

;启动设置 
numprocs=1          ;进程数
autostart=true      ;当supervisor启动时,程序将会自动启动 
autorestart=true    ;自动重启

;停止信号,默认TERM 
stopsignal=INT

[program:dev.celery.worker] 
;指定运行目录 
directory=/root/my_prj2
;运行目录下执行命令,日志保存在项目目录下
command=celery -A my_prj2 worker --loglevel info --logfile dev_celery_worker.log

;启动设置 
numprocs=1          ;进程数
autostart=true      ;当supervisor启动时,程序将会自动启动 
autorestart=true    ;自动重启

;停止信号,默认TERM 
stopsignal=INT

当然,也可以像Nginx一样配置指定路径下的配置文件集合

[include]
files = /etc/supervisor/conf.d/*.conf

注意:在需要同时启动worker和beat也需要添加整个Program,不能在一个program下使用两条command,如使用两条command,则只会执行最后一条

3. Supervisor命令

3.1 启动命令

# 指定配置文件启动
supervisord -c supervisord.conf

3.2 关闭命令

# 指定配置文件启动
supervisord -c supervisord.conf shutdown

3.3 重启命令

# 指定配置文件启动
supervisord -c supervisord.conf reload
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!