如何在芹菜任务中执行长时间运行的子进程?

tsa*_*512 5 python shell subprocess celery

我有以下代码,我在 celery 任务中使用子进程运行 shell 脚本。它不起作用,因为我没有收到错误或任何前进进度,或 celery 任务的任何输出:

以下是执行任务的代码:

def run_shell_command(command_line):
    command_line_args = shlex.split(command_line)

    logging.info('Subprocess: "' + command_line + '"')

    try:
        command_line_process = subprocess.Popen(
            command_line_args,
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        )

        for l in iter(command_line_process.stdout.readline,b''):
            print l.strip()

        command_line_process.communicate()
        command_line_process.wait()

    except (OSError, subprocess.CalledProcessError) as exception:
        logging.info('Exception occured: ' + str(exception))
        logging.info('Subprocess failed')
        return False
    else:
        # no exception was raised
        logging.info('Subprocess finished')

    return True
Run Code Online (Sandbox Code Playgroud)

它是从任务中调用的:

@app.task
def execute(jsonConfig, projectName, tagName, stage, description):

     command = 'python ' + runScript + ' -c ' + fileName
     run_shell_command(command)
Run Code Online (Sandbox Code Playgroud)

这里python“runScript”本身就是调用子进程,并执行一个长时间运行的任务。可能是什么问题呢

日志记录级别已设置为 INFO :

logging.basicConfig(filename='celery-execution.log',level=logging.INFO)
Run Code Online (Sandbox Code Playgroud)

celery worker 启动如下:

celery -A celery_worker worker --loglevel=info
Run Code Online (Sandbox Code Playgroud)

我可以看到正在启动的子进程:

[2016-05-03 01:08:55,126: INFO/Worker-2] Subprocess: "python runScript.py -c data/confs/Demo-demo14-1.conf"
Run Code Online (Sandbox Code Playgroud)

我还可以看到使用 ps -ef 在后台运行的子进程,但是这是一个计算/内存密集型工作负载,它似乎实际上并没有使用任何 CPU 或内存,这让我相信没有真正发生任何事情并且它卡住了.