#!/EMCDisk1/home/software/Python-3.6.12/bin/python
import argparse
import subprocess
# 添加命令行参数
parser = argparse.ArgumentParser(description="Start a Celery worker and monitor its output.")
parser.add_argument('worker', help="Name of the worker.")
args = parser.parse_args()
worker = args.worker
cmd = f'/EMCDisk1/home/software/Python-3.6.12/bin/celery -A tasks worker -l info -n {worker} --loglevel=info --concurrency=5'
with open(f'{worker}.log','a') as f:
while True:
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,bufsize=0,universal_newlines=True)
while p.poll() is None:
stdout = p.stdout.readline().strip()
if stdout:
f.writelines(stdout+'\n')
if 'CRITICAL/MainProcess' in stdout:
p.terminate()
try:
p.wait(timeout=10)
except subprocess.TimeoutExpired:
p.kill()
break
Celery自动死亡解决办法
于 2023-04-27 14:06:54 首次发布
该脚本使用Python的argparse模块处理命令行参数,启动并监控Celeryworker。它按照指定的配置(如loglevel=info,concurrency=5)运行worker,并将输出追加到日志文件中。当检测到CRITICAL/MainProcess时,脚本会终止worker进程。
摘要由CSDN通过智能技术生成