python测试开发django之使用supervisord 后台启动celery 服务(worker/beat)

 更新时间:2022年07月19日 16:41:12   作者:上海-悠悠  
Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统,这篇文章主要介绍了python测试开发django之使用supervisord 后台启动celery 服务(worker/beat),需要的朋友可以参考下

前言

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

环境准备

centos 安装 supervisord

yum install -y supervisord

debian 安装 supervisord

apt-get install -y supervisor

supervisord.conf

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

# cd /etc/supervisor
/etc/supervisor# ls
conf.d	supervisord.conf

cat 查看内容

# cat supervisord.conf 
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

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

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

echo_supervisord_conf > ./supervisord.conf

文件内容编写

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

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html

; 前面文档省略,不用删,接着后面写

[program:celery-worker]
command=celery -A hrun2_web worker -l info ; 管理命令,supervisor不支持后台进程
process_name=%(program_name)s
#user=root                                 ;进程启动用户
autostart=true                           ;是否随supervisor启动
autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
startretries=3                           ;启动尝试次数
stderr_logfile=./celery_worker.log           ;标准输出文件
stdout_logfile=./celery_worker.log        ;标准输出文件
loglevel=info                            ;日志的级别


[program:celery-beat]
command=celery -A hrun2_web beat ; 管理命令,supervisor不支持后台进程
process_name=%(program_name)s
#user=root                                 ;进程启动用户
autostart=true                           ;是否随supervisor启动
autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
startretries=3                           ;启动尝试次数
stderr_logfile=./celery_beat.log          ;标准输出文件
stdout_logfile=./celery_beat.log        ;标准输出文件
loglevel=info                            ;日志的级别

启动服务

启动服务

supervisord -c /path/supervisord.conf

关闭服务

supervisorctl shutdown

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

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

查看启动的服务

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 文件保存运行日志,所以可以直接查看这个文件

tail -f celery_worker.log -n 30

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

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

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

相关文章

  • keras分类之二分类实例(Cat and dog)

    keras分类之二分类实例(Cat and dog)

    这篇文章主要介绍了keras分类之二分类实例(Cat and dog),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • python中pika模块问题的深入探究

    python中pika模块问题的深入探究

    这篇文章主要给大家介绍了关于python中pika模块问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • OpenCV搞定腾讯滑块验证码的实现代码

    OpenCV搞定腾讯滑块验证码的实现代码

    这篇文章主要介绍了OpenCV搞定腾讯滑块验证码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 基于并发服务器几种实现方法(总结)

    基于并发服务器几种实现方法(总结)

    下面小编就为大家分享一篇基于并发服务器几种实现方法(总结),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • python协程用法实例分析

    python协程用法实例分析

    这篇文章主要介绍了python协程用法,实例分析Python中协议的概念、功能及使用方法,需要的朋友可以参考下
    2015-06-06
  • Python模块结构与布局操作方法实例分析

    Python模块结构与布局操作方法实例分析

    这篇文章主要介绍了Python模块结构与布局操作方法,结合实例形式分析了Python模块与布局的相关概念、使用方法与相关注意事项,需要的朋友可以参考下
    2017-07-07
  • Python使用unittest进行有效测试的示例详解

    Python使用unittest进行有效测试的示例详解

    这篇文章主要介绍了如何使用 unittest 来编写和运行单元测试,希望通过阅读本文,大家能了解 unittest 的基本使用方法,以及如何使用 unittest 中的断言方法和测试用例组织结构
    2023-06-06
  • Python sklearn中的.fit与.predict的用法说明

    Python sklearn中的.fit与.predict的用法说明

    这篇文章主要介绍了Python sklearn中的.fit与.predict的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • 用python写一个windows下的定时关机脚本(推荐)

    用python写一个windows下的定时关机脚本(推荐)

    由于本人经常使用笔记本共享WiFi,但是又不想笔记本开机一夜,每次都是使用dos命令关机,感觉好麻烦,然后小编想到用python写一个定时关机的脚本,具体实例代码请参考本文
    2017-03-03
  • numpy创建神经网络框架

    numpy创建神经网络框架

    本文介绍了使用numpy从零搭建了一个类似于pytorch的深度学习框架,可以用在很多地方,有需要的朋友可以自行参考一下
    2021-08-08

最新评论