经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Django » 查看文章
python测试开发django之使用supervisord?后台启动celery?服务(worker/beat)
来源:jb51  时间:2022/7/19 19:04:54  对本文有异议

前言

Supervisor(‘http://supervisord.org/’)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
它可以很方便的监听、启动、停止、重启一个或多个进程。
用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

环境准备

centos 安装 supervisord

  1. yum install -y supervisord

debian 安装 supervisord

  1. apt-get install -y supervisor

supervisord.conf

安装完成后在/etc/supervisor 目录下会有个配置文件 supervisord.conf

  1. # cd /etc/supervisor
  2. /etc/supervisor# ls
  3. conf.d supervisord.conf

cat 查看内容

  1. # cat supervisord.conf
  2. ; supervisor config file
  3.  
  4. [unix_http_server]
  5. file=/var/run/supervisor.sock ; (the path to the socket file)
  6. chmod=0700 ; sockef file mode (default 0700)
  7.  
  8. [supervisord]
  9. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  10. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  11. childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
  12.  
  13. ; the below section must remain in the config file for RPC
  14. ; (supervisorctl/web interface) to work, additional interfaces may be
  15. ; added by defining them in separate rpcinterface: sections
  16. [rpcinterface:supervisor]
  17. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  18.  
  19. [supervisorctl]
  20. serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
  21.  
  22. ; The [include] section can just contain the "files" setting. This
  23. ; setting can list multiple files (separated by whitespace or
  24. ; newlines). It can also contain wildcards. The filenames are
  25. ; interpreted as relative to this file. Included files *cannot*
  26. ; include files themselves.
  27.  
  28. [include]
  29. files = /etc/supervisor/conf.d/*.conf

在项目中我们需要用到此配置,可以在项目根目录导出一份

  1. echo_supervisord_conf > ./supervisord.conf

文件内容编写

supervisord.conf文件内容编写, 前面内容不用改,直接接着在后面写
比如我需要后台启动celery的worker和beat服务

  1. ; Sample supervisor config file.
  2. ;
  3. ; For more information on the config file, please see:
  4. ; http://supervisord.org/configuration.html
  5.  
  6. ; 前面文档省略,不用删,接着后面写
  7.  
  8. [program:celery-worker]
  9. command=celery -A hrun2_web worker -l info ; 管理命令,supervisor不支持后台进程
  10. process_name=%(program_name)s
  11. #user=root ;进程启动用户
  12. autostart=true ;是否随supervisor启动
  13. autorestart=true ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
  14. startretries=3 ;启动尝试次数
  15. stderr_logfile=./celery_worker.log ;标准输出文件
  16. stdout_logfile=./celery_worker.log ;标准输出文件
  17. loglevel=info ;日志的级别
  18.  
  19.  
  20. [program:celery-beat]
  21. command=celery -A hrun2_web beat ; 管理命令,supervisor不支持后台进程
  22. process_name=%(program_name)s
  23. #user=root ;进程启动用户
  24. autostart=true ;是否随supervisor启动
  25. autorestart=true ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
  26. startretries=3 ;启动尝试次数
  27. stderr_logfile=./celery_beat.log ;标准输出文件
  28. stdout_logfile=./celery_beat.log ;标准输出文件
  29. loglevel=info ;日志的级别

启动服务

启动服务

  1. supervisord -c /path/supervisord.conf

关闭服务

  1. supervisorctl shutdown

运行过程,启动没问题,不显示任何内容,如果需要关闭用shutdown

  1. root@13107c465557:/code# supervisord -c ./supervisord.conf
  2. root@13107c465557:/code# supervisorctl shutdown
  3. Shut down

查看启动的服务

  1. supervisorctl status

执行结果如下

root@13107c465557:/code# supervisorctl status
celery-worker RUNNING pid 234, uptime 0:00:14
celery-beat RUNNING pid 235, uptime 0:00:14

说明启动了celery和celery-beat两个服务

查看日志

我们在配置里面指定了./celery_worker.log 文件保存运行日志,所以可以直接查看这个文件

  1. tail -f celery_worker.log -n 30

运行就可以看到worker运行的日志了

参考教程 https://www.jb51.net/article/146238.htm

到此这篇关于python测试开发django之使用supervisord后台启动celery服务(worker/beat)的文章就介绍到这了,更多相关supervisord后台启动celery服务内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号