Created
April 12, 2014 13:07
-
-
Save comynli/10534855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
process_pool(){ | |
# | |
if [ $# -lt 3 ]; then | |
echo "$0 process_num command [args]" | |
return 1 | |
fi | |
_process_num=$1 | |
shift | |
_func=$1 | |
shift | |
if [[ ! $_process_num =~ ^[0-9]+$ ]]; then | |
echo "process_num must be a number" | |
return 1 | |
fi | |
if !type $_func >/dev/null 2>&1; then | |
echo "comannd must be executable" | |
return 1 | |
fi | |
# create fifo file | |
fifo="/tmp/$$.fifo" | |
mkfifo $fifo | |
exec {FD}<>$fifo | |
rm $fifo | |
# create slot | |
for i in $(seq $_process_num); do | |
echo >&$FD | |
done | |
# run | |
for arg in $@; do | |
read -u $FD | |
{ | |
$_func $arg | |
echo >&$FD | |
}& | |
done | |
# wait | |
wait | |
# release slot fd | |
exec {FD}>&- | |
} | |
test(){ | |
echo $1 | |
sleep 30 | |
return 0 | |
} | |
process_pool 3 'test' 1 2 3 4 5 6 7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment