N = 50000
with open('input', 'w') as f:
for i in range(N):
f.write(str(i) + '\n')
run_command = '/bin/bash -e -o pipefail -c "((sort | tee >/dev/null >(cat | (tee >/dev/null >(sort >&3)))) <input 3>output)& wait"'
subprocess.check_call(run_command, shell=True)
time.sleep(sleep_time)
print sh.wc("output", "-l")
使用sleep_time = 0
运行此python代码返回0,但sleep_time = 1
返回50000。
原因似乎是不等待bash子进程完成。可能我对wait
函数的使用不正确。我做了实验,但没有找到满意的解决方案。
答案 0 :(得分:1)
当您/bin/bash
命令将在子shell中运行时,您将立即退出该过程。此子shell中运行的命令也在后台运行,因为最后的&
,您可以将$!
作为参数传递给wait
,以便等待直到最后一个后台进程退出。