• 热门专题

perl多线程及信号控制

作者:  发布日期:2015-02-04 20:30:26
Tag标签:多线  信号  
  • #!/usr/bin/perl
    use strict;
    use warnings;
    use threads;
    use Thread::Semaphore;
    
    my $max_thread = 5;
    my $semaphore = Thread::Semaphore->new($max_thread);
    
    sub TestFun
    {
    	$semaphore->up();
    }
    
    for(my $index = 1; $index <= 10; $index ++)
    {
    	$semaphore->down();
    	my $thread = threads->create(&TestFun);
    	$thread->detach();
    }
    
    WaitQuit();
    
    <pre name="code" class="plain">sub WaitQuit
    {
    	my $num = 0;
    	while($num < $max_thread)
    	{
    		$semaphore->down();
    		$num ++;
    	}
    }
    
    

     

    #!perl
    use warnings;
    use strict;
    use threads;
    use Thread::Semaphore;
    
    die "perl $0 <thread> <blastConfig> <pep || dna>
    perl $0 3 blast.config dna
    " if @ARGV != 3;
    
    my $max_thread = $ARGV[0];
    my $semaphore = Thread::Semaphore->new($max_thread);
    
    mkdir "compliantFasta";
    chdir "compliantFasta";
    
    open FA, "../$ARGV[1]" or die $!;
    while(<FA>)
    {
    	chomp;
    	my @tmp = split;
    
    	$semaphore->down();
    	my $thread = threads->create(&adjust, $tmp[0], $tmp[1]);
    	$thread->detach();
    }
    
    &wait;
    
    chdir "..";
    my $len = 0;
    my ($type, $p);
    if($ARGV[2] =~ /dna/i)
    {
    	$len = 30;
    	$type = "F";
    	$p = "blastn";
    }elsif($ARGV[2] =~ /pep/i){
    	$len = 10;
    	$type = "T";
    	$p = "blastp";
    }
    
    `orthomclFilterFasta compliantFasta $len 20`;
    `formatdb -i goodProteins.fasta -p $type`;
    mkdir "splitBlast";
    `perl split_fasta.pl goodProteins.fasta 8 splitBlast/blast`;
    my @blast = `ls splitBlast/*`;
    my $sp2 = Thread::Semaphore->new(8);
    foreach my $i(@blast)
    {
    	chomp($i);
    	$sp2->down();
    	my $thread = threads->create(&blast, $i, $p, $ARGV[0]);
    	$thread->detach();
    }
    
    &wait2;
    
    `cat splitBlast/*.out > all.blast`;
    `orthomclBlastParser all.blast compliantFasta > similarSequences.txt`;
    `rm all.blast -rf`;
    
    sub blast
    {
    	my ($f, $t, $p) = @_;
    	`blastall -p $t -i $f -d goodProteins.fasta -m 8 -F F -b 1000 -v 1000 -a $p -o $f.out`;
    	$sp2->up();
    }
    
    sub wait2
    {
    	my $num = 0;
    	while($num < 8)
    	{
    		$sp2->down();
    		$num ++;
    	}
    }
    
    sub adjust
    {
    	my ($label, $path) = @_;
    	`orthomclAdjustFasta $label $path 1`;
    	$semaphore->up();
    }
    
    sub wait
    {
    	my $num = 0;
    	while($num < $max_thread)
    	{
    		$semaphore->down();
    		$num ++;
    	}
    }

     

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规