我如何需要并行提交多个工作(比如说每批10个工作)并等待它们完成,然后重新提交下一个十个......?
array=( $(ls -1 window/*realign_win*.txt ) ) ;
echo ${#array[@]};
#for e in ${!array[*]}
for (( e=0; e<="${#array[@]}"; e++ ))
do
# echo "$e" ;
for n in {0..9}
do
if [[ $e -gt ${#array[@]} ]]
then
echo $e, ${#array[*]};
break;
else
echo $e, ${#array[*]};
j=$(($e+$n)) ;
echo "didel-1.01-linux-64bit --analysis indels --doDiploid --bamFile $i --ref Homo_sapiens.GRCh37.62.fa --varFile ${array[$j]} --libFile ${i}_didel_output.libraries.txt --outputFile ${array[$j]}.didel_stage3" ;
#e=$(($e+1)) ;
echo $e;
fi
done &
wait
done
done
请提前建议
提出建议答案 0 :(得分:4)
Parallel::ForkManager可用于并行化Perl中的工作,并告诉您限制同时工作的最大数量。
use Parallel::ForkManager qw( );
use constant MAX_WORKERS => 10;
my $pm = Parallel::ForkManager->($MAX_PROCESSES);
for my $item (@work) {
my $pid = $pm->start() and next;
...
$pm->finish();
}
threads和forks提供了备用界面。模块的选择会影响是否使用线程或子进程。
use threads; # or: use forks;
use Thread::Queue qw( );
use constant MAX_WORKERS => 10;
my $q = Thread::Queue->new();
my @threads;
for (1..MAX_WORKERS) {
push @threads, async {
while (my $item = $q->dequeue()) {
...
}
};
}
$q->enqueue(@work);
$q->enqueue(undef) for 1..@threads;
$_->join() for @threads;
答案 1 :(得分:3)
仍在使用shell脚本,就像这样。
#!/bin/sh
jobs=`jot 25`
echo $jobs
set -- $jobs
while [ $# -gt 1 ]; do
pids=""
for i in `jot 10`; do
[ $# == 0 ] && break
job=$1
shift
echo start $job && sleep 3 && echo finish $job &
pids="$pids $!"
done
wait $pids
done
echo done
编辑将continue
更改为break
。谢谢@glennjackman