我想在一个bash文件中运行多并行任务,如下面的示例代码,
for i in 1 2 3 4 5 6 7 8; do
setsid python /tmp/t.py ${i} 1>>/tmp/1.log 2>&1 &
done
wait # first wait
echo "next wait"
for i in 9 10 11 12 13 14 15 16; do
setsid python /tmp/t.py ${i} 1>>/tmp/1.log 2>&1 &
done
wait # second wait
正如您所见,是
wait
可能做到这一点? 我想先运行前8个任务,然后运行
wait
完成所有任务,然后由于RAM受限制而产生接下来的8个任务,我无法在一轮中运行全部16个任务。
最新回答
- 2021-1-51 #
- 2021-1-52 #
类似这样的事情:
set -a pids for i in {1..8} do python /tmp/t.py ${i} 1>>/tmp/1.log 2>&1 & pids[${#pids[*]}]=$! # Remember pid of child done # Wait for each child. This loops exits when all children are finished for pid in ${pids[*]} do wait $pid printf "Sensed end of %d\n" $pid done ### Continue processing here
- 2021-1-53 #
使用GNU Parallel,它看起来像这样:
parallel -j8 setsid python /tmp/t.py {} ::: {1..16} > log
相关问题
- 在bash中并行运行有限数量的子进程?bashparallelprocessing2021-01-09 00:24
使用选项
-w
或--wait
到setsid
这样,setsid命令将等到python进程结束后再结束自身.这意味着外壳wait
命令现在有等待子进程。