perl 多线程及信号控制

#!/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 ++;
	}
}
时间: 2024-08-27 23:56:34

perl 多线程及信号控制的相关文章

PHP 共享内存使用与信号控制

共享内存 共享内存的使用主要是为了能够在同一台机器不同的进程中共享一些数据,比如在多个 php-fpm 进程中共享当前进程的使用情况.这种通信也称为进程间通信(Inter-Process Communication),简称 IPC. PHP 内置的 shmop 扩展 (Shared Memory Operations) 提供了一系列共享内存操作的函数(可能是用的人不多吧,这一块儿的文档还没有中文翻译).在 Linux 上,这些函数直接是通过调用 shm* 系列的函数实现,而 Winodows 上

多线程顺序的控制(wait,notity,sleep)

public class abc extends Thread{    private Object prev=null;    private Object self=null;    private String msg=null;    public abc(Object prev,Object self,String msg){        this.prev=prev;        this.self=self;        this.msg=msg;    }    publi

Nginx 的信号控制

摘自:Nginx服务器初识:Nginx启动.停止与信号控制 名称 功能 说明 HUP 重启   QUIT 从容关闭   TERM 快速关闭   INT 从容关闭   USR1 切换日志文件 通常用在切换日志或切割日志文件中用到 USR2 平滑升级可执行进程 低版本升级为高版本 WINCH 从容关闭工作进程 work process

perl多线程

2000 年 5 月发布的 Perl v5.6.0 中开始引入了一个全新的线程模型,即 interpreter threads, 或称为 ithreads,也正是在这个版本的发布申明中第一次提出了 5005threads 线程模型将来可能会被禁用的问题. perl线程的生命周期 创建线程 线程作为Perl中的一种实体,其周期包括创建,运行和退出. Perl里创建一个新的线程非常简单,主要有两种方法,他们分别是: 1.使用threads包的create方法 use threads; sub say

nginx 的信号控制概述

<nginx 在ubuntu 上的启动,停止,重启>中的停止和重启命令基本都是用信号来控制的.这是一些简单的信号控制. 在Nginx服务器中,通常情况都是通过对其发送控制信号进行控制的,除了以上所说的简单信号控制之外,还有很多的信号控制.在此,我们需要知道一些常见的信号控制命令. Nginx常见的信号控制: HUP 重启 QUIT 从容关闭 TERM 快速关闭 INT 从容关闭 USR1 切换日志文件 USR2 平滑升级可执行进程 WINCH 从容关闭工作进程 sudo kill –WINCH

20181203使用信号控制进程

使用信号控制进程 kill(只能跟PID或JOB ID),killall(name),pgrep,pkill,top给进程发送信号[[email protected] ~]# kill -l //列出所有支持的信号编号 信号名1) SIGHUP 重新加载配置 PID 不变2) SIGINT 键盘中断^C3) SIGQUIT 键盘退出9) SIGKILL 强制终止15) SIGTERM 终止(正常结束),缺省信号,允许去释放资源18) SIGCONT 继续19) SIGSTOP 停止20)SIGT

Nginx的信号控制

信号列表: TERM, INT Quick shutdown QUIT Graceful shutdown  优雅的关闭进程,即等请求结束后再关闭 HUP Configuration reload ,Start the new worker processes with a new configuration Gracefully shutdown the old worker processes 改变配置文件,平滑的重读配置文件 USR1 Reopen the log files 重读日志,在

C#中多线程信号控制ManualResetEvent和AutoResetEvent

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication22 { publ

perl多线程理解

Thread:在使用多线程处理比较大的数据量的扫描,遇到读写文件可能死锁的问题. Perl 线程的生命周期 1.使用 threads 包的 create() 方法: use threads; sub say_hello { printf("Hello thread! @_.\n"); return( rand(10) ); } my $t1 = threads->create( \&say_hello, "param1", "param2&q