Created
August 7, 2013 09:39
-
-
Save kernelhcy/6172635 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 | |
# | |
# 多个进行并行执行任务 | |
# | |
SEND_THREAD_NUM=8 | |
tmp_fifofile="/tmp/$$.fifo" # 脚本运行的当前进程ID号作为文件名 | |
mkfifo "$tmp_fifofile" # 新建一个随机fifo管道文件 | |
exec 6<>"$tmp_fifofile" # 定义文件描述符6指向这个fifo管道文件 | |
rm $tmp_fifofile | |
for ((i=0;i<$SEND_THREAD_NUM;i++));do | |
echo # for循环往fifo管道文件中写入8个空行 | |
done >&6 | |
for f in `ls assets/*.png`;do # 100 次 for 循环 开始 | |
read -u6 # 从文件描述符6中读取行(实际指向fifo管道) | |
{ | |
echo $f # 打印f | |
optipng -o7 $f # 以压缩png为例 | |
echo >&6 # 再次往fifo管道文件中写入一个空行。 | |
} & | |
# {} 这部分语句被放入后台作为一个子进程执行,所以不必每次等待3秒后执行 | |
#下一个,这部分的echo $i几乎是同时完成的,当fifo中8个空行读完后 for循环 | |
# 继续等待 read 中读取fifo数据,当后台的13个子进程等待3秒后,按次序 | |
# 排队往fifo输入空行,这样fifo中又有了数据,for语句继续执行 | |
pid=$! #打印最后一个进入后台的子进程id | |
echo $pid | |
done | |
wait | |
exec 6>&- #删除文件描述符6 | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment