Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit number of threads/sub-processes used in a function in bash

Tags:

bash

My question is how change this code so it will use only 4 threads/sub-processes?

TESTS="a b c d e"

for f in $TESTS; do
  t=$[ ( $RANDOM % 5 )  + 1 ]
  sleep $t && echo $f $t &
done
wait
like image 630
mentatkgs Avatar asked Jun 28 '11 19:06

mentatkgs


2 Answers

I have found another solution for this question using parallel (part of moreutils package.)

parallel -j 4 -i bash -c "echo start {}; sleep 2; echo done {};" -- $(seq 10)

-j 4 stands for -j maxjobs

-i uses the parameters as {}

-- delimits your arguments

The output of this command will be:

start 3
start 4
start 1
start 2
done 4
done 2
done 3
done 1
start 5
start 6
start 7
start 8
done 5
done 6
start 9
done 7
start 10
done 8
done 9
done 10
like image 98
Lynch Avatar answered Dec 28 '22 17:12

Lynch


Quick and dirty solution: insert this line somewhere inside your for loop:

while [ $(jobs | wc -l) -ge 4 ] ; do sleep 1 ; done

(assumes you don't already have other background jobs running in the same shell)

like image 24
mob Avatar answered Dec 28 '22 18:12

mob