perl 多线程及信号控制

来源:互联网 发布:保健品网络推广策划 编辑:程序博客网 时间:2024/06/08 15:58
[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4. use threads;  
  5. use Thread::Semaphore;  
  6.   
  7. my $max_thread = 5;  
  8. my $semaphore = Thread::Semaphore->new($max_thread);  
  9.   
  10. sub TestFun  
  11. {  
  12.     $semaphore->up();  
  13. }  
  14.   
  15. for(my $index = 1; $index <= 10; $index ++)  
  16. {  
  17.     $semaphore->down();  
  18.     my $thread = threads->create(\&TestFun);  
  19.     $thread->detach();  
  20. }  
  21.   
  22. WaitQuit();  
  23.   
  24. <pre name="code" class="plain">sub WaitQuit  
  25. {  
  26.     my $num = 0;  
  27.     while($num < $max_thread)  
  28.     {  
  29.         $semaphore->down();  
  30.         $num ++;  
  31.     }  
  32. }  


[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #!perl  
  2. use warnings;  
  3. use strict;  
  4. use threads;  
  5. use Thread::Semaphore;  
  6.   
  7. die "perl $0 <thread> <blastConfig> <pep || dna>  
  8. perl $0 3 blast.config dna\n" if @ARGV != 3;  
  9.   
  10. my $max_thread = $ARGV[0];  
  11. my $semaphore = Thread::Semaphore->new($max_thread);  
  12.   
  13. mkdir "compliantFasta";  
  14. chdir "compliantFasta";  
  15.   
  16. open FA, "../$ARGV[1]" or die $!;  
  17. while(<FA>)  
  18. {  
  19.     chomp;  
  20.     my @tmp = split;  
  21.   
  22.     $semaphore->down();  
  23.     my $thread = threads->create(\&adjust, $tmp[0], $tmp[1]);  
  24.     $thread->detach();  
  25. }  
  26.   
  27. &wait;  
  28.   
  29. chdir "..";  
  30. my $len = 0;  
  31. my ($type, $p);  
  32. if($ARGV[2] =~ /dna/i)  
  33. {  
  34.     $len = 30;  
  35.     $type = "F";  
  36.     $p = "blastn";  
  37. }elsif($ARGV[2] =~ /pep/i){  
  38.     $len = 10;  
  39.     $type = "T";  
  40.     $p = "blastp";  
  41. }  
  42.   
  43. `orthomclFilterFasta compliantFasta $len 20`;  
  44. `formatdb -i goodProteins.fasta -p $type`;  
  45. mkdir "splitBlast";  
  46. `perl split_fasta.pl goodProteins.fasta 8 splitBlast/blast`;  
  47. my @blast = `ls splitBlast/*`;  
  48. my $sp2 = Thread::Semaphore->new(8);  
  49. foreach my $i(@blast)  
  50. {  
  51.     chomp($i);  
  52.     $sp2->down();  
  53.     my $thread = threads->create(\&blast, $i, $p, $ARGV[0]);  
  54.     $thread->detach();  
  55. }  
  56.   
  57. &wait2;  
  58.   
  59. `cat splitBlast/*.out > all.blast`;  
  60. `orthomclBlastParser all.blast compliantFasta > similarSequences.txt`;  
  61. `rm all.blast -rf`;  
  62.   
  63. sub blast  
  64. {  
  65.     my ($f, $t, $p) = @_;  
  66.     `blastall -p $t -i $f -d goodProteins.fasta -m 8 -F F -b 1000 -v 1000 -a $p -o $f.out`;  
  67.     $sp2->up();  
  68. }  
  69.   
  70. sub wait2  
  71. {  
  72.     my $num = 0;  
  73.     while($num < 8)  
  74.     {  
  75.         $sp2->down();  
  76.         $num ++;  
  77.     }  
  78. }  
  79.   
  80. sub adjust  
  81. {  
  82.     my ($label, $path) = @_;  
  83.     `orthomclAdjustFasta $label $path 1`;  
  84.     $semaphore->up();  
  85. }  
  86.   
  87. sub wait  
  88. {  
  89.     my $num = 0;  
  90.     while($num < $max_thread)  
  91.     {  
  92.         $semaphore->down();  
  93.         $num ++;  
  94.     }  
  95. }  
0 0
原创粉丝点击