码迷,mamicode.com
首页 > 编程语言 > 详细

perl 多线程及信号控制

时间:2015-02-04 13:03:43      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

#!/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\n" 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 ++;
	}
}


perl 多线程及信号控制

标签:

原文地址:http://blog.csdn.net/skenoy/article/details/43483419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!