同时启动两个bash命令并从之前完成的过程中收集结果

问题描述 投票:0回答:1

假设我想从我的Python脚本中运行两个bash命令c1c2,这些命令基本上处理(但不能修改)Linux上相同的数据(因此这不是微妙的竞争条件)。

现在我想同时启动它们(使用os.system),看看哪一个更快完成,一旦一个进程完成,我将收集它的输出(可以用c1 >> log1.txt转储到一个文件中),并终止另一个进程。

请注意,两个过程的处理时间可能大不相同,因此可观察到,例如一个需要十秒钟,而另一个需要60秒。

python subprocess
1个回答
2
投票

无线程工作的方法可能类似于:

import os, subprocess, tempfile

def get_first_outfile(inputFileName):
    p1_stdout = tempfile.NamedTemporaryFile()
    p2_stdout = tempfile.NamedTemporaryFile()
    p1 = subprocess.Popen(['process-one', inputFileName], stdout=p1_stdout)
    p2 = subprocess.Popen(['process-two', inputFileName], stdout=p2_stdout)

    while True:
        if p1.poll() is not None:  # Process 1 finished first
            (file_keep, file_kill) = p1_stdout, p2_stdout
            (proc_keep, proc_kill) = p1, p2
            break
        if p2.poll() is not None:  # Process 2 finished first
            (file_keep, file_kill) = p2_stdout, p1_stdout
            (proc_keep, proc_kill) = p2, p1
            break
        time.sleep(0.1)           # Wait to poll again

    proc_kill.terminate()         # Terminate the process that didn't finish
    file_keep.seek(0)             # Rewind the output file we want to keep to the beginning
    file_kill.close()             # closing a tempfile deletes it

    if finished_first.returncode != 0:
        raise Exception("Process failed with status %r" % finished_first.returncode)
    return keep_outfile           # Return the output file we want to keep

或者,可以让Python调用shell并在那里完成工作:

shellScript = '''
outOne=$(mktemp -t out.XXXXXX) || exit
outTwo=$(mktemp -t out.XXXXXX) || exit
processOne "$@" >"$outOne" & proc1_pid=$!
processTwo "$@" >"$outTwo" & proc2_pid=$!
wait -n # wait for one of the two processes to finish
if kill -0 "$proc1_pid"; then
  # proc1 is still running, so proc2 finished
  cat -- "$outTwo"
  kill "$proc1_pid"
else
  cat -- "$outOne"
  kill "$proc2_pid"
fi
'''

def get_output_from_first_process(inputFileName):
    return subprocess.check_output(['bash', '-c', shellScript, '_', inputFileName],
                                   stdout=subprocess.PIPE)

请注意,我们仍然没有使用os.system(),也没有使用shell=Truewait -n是一个bash扩展,因此我们需要确保bash - 而不是/bin/sh - 可用于它的功能。

© www.soinside.com 2019 - 2024. All rights reserved.