![](/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.