首页>Computer>source

我想在一个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-5
    1 #

    使用选项 -w--waitsetsid 这样,setsid命令将等到python进程结束后再结束自身.这意味着外壳 wait 命令现在有等待子进程。

  • 2021-1-5
    2 #

    类似这样的事情:

    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-5
    3 #

    使用GNU Parallel,它看起来像这样:

    parallel -j8 setsid python /tmp/t.py {} ::: {1..16} > log
    

  • pivot table:LibreOffice等效于SQL" some_column的count(*)组"
  • linux:几周内未收到有关Ubuntu 2004-wSL 2的更新