并行bash脚本与最大进程数 [英] Parallelize Bash Script with maximum number of processes
问题描述
可以说我有在bash一个循环:
在'一些,command`富
做
做多岁的$ foo的
DONE
做出头
是CPU绑定,并且我有一个很好的光泽4核处理器。我希望能够运行多达4 做出头的一次。
天真的方法似乎是:
在'一些,command`富
做
做多岁的$ foo的&安培;
DONE
这将运行的所有的做出头
■在一次,但有几个缺点,主要是那些几岁可能也有一定的显著I / O其中,执行的所有的一次可能会慢一点。另一个问题是,这个code座立即返回,所以没办法的事,当所有的做出头
,则完成其他工作。
你会怎样写这个循环,这样总有那么点¯x做出头
正在竞选一次?
maxjobs = 4
并行(){
而[$#-gt 0];做
jobcnt =(`工作-p`)
如果[$ {#jobcnt [@]} $ -lt maxjobs]。然后
做多岁的$ 1
转移
科幻
DONE
等待
}并行ARG1 ARG25 args设置为第三职业ARG4 ...
Lets say I have a loop in bash:
for foo in `some-command`
do
do-something $foo
done
do-something
is cpu bound and I have a nice shiny 4 core processor. I'd like to be able to run up to 4 do-something
's at once.
The naive approach seems to be:
for foo in `some-command`
do
do-something $foo &
done
This will run all do-something
s at once, but there are a couple downsides, mainly that do-something may also have some significant I/O which performing all at once might slow down a bit. The other problem is that this code block returns immediately, so no way to do other work when all the do-something
s are finished.
How would you write this loop so there are always X do-something
s running at once?
maxjobs=4 parallelize () { while [ $# -gt 0 ] ; do jobcnt=(`jobs -p`) if [ ${#jobcnt[@]} -lt $maxjobs ] ; then do-something $1 & shift fi done wait } parallelize arg1 arg2 "5 args to third job" arg4 ...
这篇关于并行bash脚本与最大进程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!