bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange behaviour from jobs -p in a subshell


From: Greg Wooledge
Subject: Re: Strange behaviour from jobs -p in a subshell
Date: Wed, 14 Nov 2018 10:25:05 -0500
User-agent: NeoMutt/20170113 (1.7.2)

> >>> Consider the following script. While the 3 sleeps are running, both jobs
> >>> -p and $(jobs -p) will print 3 PIDs. Once the 3 children are finished,
[...]

... hey, I think I just figured out the GOAL!

You want to run a whole bunch of jobs in parallel, but only 3 at a
time.  Right?  There are some solutions for that at
<https://mywiki.wooledge.org/ProcessManagement> (Advanced questions #4).

Our bot in IRC also suggests this example:

parallel() {
  local workers=$1 handler=$2 w i
  shift 2
  local elements=("$@")
  for (( w = 0; w < workers; ++w )); do
    for (( i = w; i < ${#elements[@]}; i += workers )); do
      "$handler" "${elements[i]}"
    done &
  done
  wait
}
parallel 5 md5 *.txt

Some of the examples on the wiki page have a stricter guarantee of
maintaining the number of jobs when their durations are unpredictable,
but I include the above example for completeness.  Choose whichever
solution you like best.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]