Supervisor:使用

本文使用supervisor来管理python进程。

首先创建一个python工程,命名为super27, 新建文件 call.py ,文件内容:

import datetime
import os
import time
import logging

while True:
    logging.basicConfig(level=logging.NOTSET)
    information = '%s RUN %s' % (os.getpid(), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    logging.info(information)
    time.sleep(3)

这个文件内容很简单,每隔三秒输出进程号和当前时间。

将这个项目放在 /root 目录下,这个文件的全路径是 /root/super27/call.py 。

编辑supervisor的配置文件,在 /etc 目录下supervisord.conf 添加下面的配置信息

[program:superman]
command=python /root/super27/call.py
numprocs=5
numprocs_start=5
process_name=%(program_name)s_%(process_num)s

redirect_stderr=true
stdout_logfile=/root/super27/result.out

numprocs是指启动5个进程,并且将日志信息输出到result.out中。

启动supervisor,可以看到进程已经启动:

查看result.out文件:

可以看到日志中每隔三秒就会输出进程号和当前时间。

自此完成supervisor的使用。

如何在virtualenv环境中使用supervisor

pip install virtaulenv

virtualenv -p /usr/bin/python venv

source venv/bin/activate

vim /etc/supervisord.conf

supervisord -c /etc/supervisord.conf

自此就完成了在virtualenv环境中启动了supervisor。

原文地址:https://www.cnblogs.com/colin220/p/10856242.html