shell 多进程使用

实测发现也就那样,在有sleep的时候确实比for循环快,但是把sleep 去掉就没有for快了,真垃圾,根据场景选择

real    1m53.050s
user    0m0.589s
sys    0m1.061s
[root@localhost ~]# time for i in `seq 1 100`;do curl -s -I "http://www.baidu.com/"|wc -l;sleep 1; done

 

#!/bin/bash
THREAD_NUM=5
TMP_FILE="/tmp/$$.fifo"

trap "exec 6>&-;exec 6<&-;exit 0" 2    

mkfifo ${TMP_FILE}  
exec 6<>${TMP_FILE} 
rm -rf ${TMP_FILE}
 
for ((J=1;J<=${THREAD_NUM};J++)) 
do
    echo >&6
done

localfunc(){
   #echo "${I}: success" 
   curl -s -I "http://www.baidu.com/"|wc -l
   sleep 1
}

for ((I=1;I<=100;I++))
do 
    read -u6 
    {
        #echo "${I}: success" ; sleep 1
        localfunc
        echo >&6    
    }&
done
wait

 
exec 6>&-
exec 6<&-



real	0m24.407s
user	0m0.576s
sys	0m1.059s
time sh shss.sh

 

参考:https://www.dazhuanlan.com/realpanda/topics/978898

https://www.cnblogs.com/old-path-white-cloud/p/11685558.html

posted @ 2021-11-19 17:21  JvvYou  阅读(58)  评论(0编辑  收藏  举报