脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python测试开发django之使用supervisord 后台启动celery 服务(worker/beat)

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

2022-07-19 19:40上海-悠悠 Python

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

?
1
yum install -y supervisord

debian 安装 supervisord

?
1
apt-get install -y supervisor

supervisord.conf

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

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

cat 查看内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

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

?
1
echo_supervisord_conf > ./supervisord.conf

文件内容编写

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
; 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                            ;日志的级别

启动服务

启动服务

?
1
supervisord -c /path/supervisord.conf

关闭服务

?
1
supervisorctl shutdown

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

?
1
2
3
root@13107c465557:/code# supervisord -c ./supervisord.conf
root@13107c465557:/code# supervisorctl shutdown
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运行的日志了

参考教程 http://www.tuohang.net/article/151311.html

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

原文链接:https://blog.csdn.net/qq_27371025/article/details/125855200

延伸 · 阅读

精彩推荐
  • Pythonpython 利用pandas将arff文件转csv文件的方法

    python 利用pandas将arff文件转csv文件的方法

    今天小编就为大家分享一篇python 利用pandas将arff文件转csv文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    lonely_square_three10982021-05-27
  • PythonPython实现自动为照片添加日期并分类的方法

    Python实现自动为照片添加日期并分类的方法

    这篇文章主要介绍了Python实现自动为照片添加日期并分类的方法,涉及Python针对文件与目录的遍历、判断、修改、复制及文件属性的相关操作技巧,需要的朋...

    nxlhero5772020-12-10
  • PythonPython实现读取HTML表格 pd.read_html()

    Python实现读取HTML表格 pd.read_html()

    这篇文章主要介绍了Python实现读取HTML表格 pd.read_html(),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    SpikeKing3922022-07-14
  • Python详解python破解zip文件密码的方法

    详解python破解zip文件密码的方法

    这篇文章主要介绍了python破解zip文件密码的方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 ...

    阿优乐扬13662020-04-27
  • PythonOpenCV图像处理之自定义滤波

    OpenCV图像处理之自定义滤波

    滤波处理分为两大类:线性滤波和非线性滤波,OpenCV里有这些滤波的函数,使用起来非常方便,这篇文章主要给大家介绍了关于OpenCV图像处理之自定义滤波的相...

    飞鸢逐浪8362021-12-24
  • PythonDjango 跨域请求处理的示例代码

    Django 跨域请求处理的示例代码

    本篇文章主要介绍了Django 跨域请求处理的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    低调的人儿7402021-02-08
  • PythonPython列表切片操作实例总结

    Python列表切片操作实例总结

    这篇文章主要介绍了Python列表切片操作,结合实例形式总结分析了Python列表切片常见操作技巧与注意事项,需要的朋友可以参考下...

    deniro_li5732021-05-30
  • Pythonpython使用正则筛选信用卡

    python使用正则筛选信用卡

    这篇文章主要为大家详细介绍了python使用正则筛选信用卡,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    水似冰7492021-05-23