美文网首页
【perl语言】利用threads实现多线程运算

【perl语言】利用threads实现多线程运算

作者: 群体遗传学 | 来源:发表于2021-02-23 09:18 被阅读0次

    代码如下

    #!/usr/bin/env perl 
    use strict;
    use threads;
    use Thread::Semaphore;
    
    my $trait = $ARGV[0];
    my $NumOfTrait = 0;
    my $thread;
    my $max_cpu = 28;
    my $semaphore = new Thread::Semaphore($max_cpu);
    my (@line, @line, %traitDict, $i);
    
    open REF, "taxa_".$trait.".txt";
    while(<REF>){
      chomp;
      @line = split /\t/;
      $traitDict{$line[0]} = $line[1];
      $NumOfTrait ++;
    }
    
    # check num of trait <> 28
    for($i=1; $i<=$NumOfTrait; $i++){
      $semaphore->down();
      $thread = threads->new(\&run_gemma, $trait, $i, $traitDict{$i});
      $thread->detach();
    }
    #
    &waitquit();
    
    # functions 
    sub waitquit{
      my ($i);
      for($i=0;$i<$max_cpu;$i++){
        $semaphore->down();
      }
      $semaphore->up($max_cpu);
    }
    
    sub run_gemma{
      my($phe, $col, $name) = @_;
      system("bash ../02.gemma.sh Bna171_".$phe." ".$col." ".$name);
      print "[INFO] $name is done.\n";
      $semaphore->up();
    }
    

    相关文章

      网友评论

          本文标题:【perl语言】利用threads实现多线程运算

          本文链接:https://www.haomeiwen.com/subject/ultrfltx.html