2011-10-11 55 views
1

我该如何需要并行提交多个作业(比如说每批十个作业),然后等待他们完成,然后重新提交下一个十个....?脚本提交十个工作,等待完成并重新提交下十个过程来完成?

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 

请给提前

回答

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 

编辑变化continuebreak。谢谢@glennjackman

+1

'break'会比'continue'更好。 –

+0

@glennjackman你是对的。 'continue'仍然会继续执行任何剩余的'for i ...'迭代,只是当它返回到'continue'时返回到循环的顶部。 break会立即跳出循环。 – AFresh1

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(); 
} 

threadsforks提供备选接口。模块的选择会影响是否使用线程或子进程。

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 [email protected]; 
$_->join() for @threads;