![](/img/trans.png)
[英]Python subprocess.check_call works in a different way than bash
[英]Python subprocess.check_call of the bash script is not waiting for subprocesses to finish
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代码sleep_time = 0
将返回0,但在sleep_time = 1
返回50000。
原因似乎在于不等待bash子进程完成。 可能我对wait
函数的使用不正确。 我进行了实验,但没有找到令人满意的解决方案。
当您/bin/bash
您的命令将在子shell中运行,因此您将立即退出该过程。 同样,您在该子Shell中运行的命令也在后台运行,因为&
末尾,您可以传递$!
作为参数,以wait
它要等到最后一个后台进程退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.