`
bluerose
  • 浏览: 145789 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Perl多线程

    博客分类:
  • Perl
阅读更多

 

 
其实使用perl的多线程很简单:
 
use Thread;
#use threads::shared;
 
my @threads;
 
open(MHO,$mhofile);
my @mholist=<MHO>;
 
foreach my $mho (@mholist) {
 next unless defined $mho;
         print "start one thread";
          $threads[$tempcount]=Thread->new(\&start_thread,$mho);
           $tempcount++;
                                            }
foreach my $thread (@threads) {
 $thread->join();
}
 
sub start_thread{
 my ($infomho)=@_;
 print "in thread $infomho";
 sleep 20;
                           }
以上就是一个多线程的例子,创建线程的方法有好几种 (比如 create(...)) 启 动了多干个线程后,我们这里一定要使用变量保存thread的id。因为,创建一个thread以后要用join取得该thread的返回值,然后系统才 会对thread进行清理,否则所有thread的信息都会保留下来,当然越积越多了。对返回值不关心的时候要用detach显式剥离该thread。所 以,在最后我们要等待这些线程的完全退出:
foreach my $thread (@threads) {
 $thread->join();
}

否则,threads库会在最后打印如下信息:
A thread exited while 2 threads were running .
 

 

Perl中的多线程的实现一般有两种办法,而老版本的办法实际上是一种多进程的办法。

一   Thread->New

该办法是传统的老办法,它与folk很类似,新建一个进程时,会把当前内存空间的所有变量都复制一份传到新的进程里面。已实现共享数据。而随着技术的发展,本文不针对该方法做深入研究。

二   IThread

这种方法是通过新建一个新的 perl interpreter 。默认情况下,所有的数据和变量是不被线程共享的。如果想共享一个变量,需通过 threads::shared 来实现。在使用此方法的时候,需要注意以下三点:

  1. 变量默认是不在线程中共享的。
  2. 通过 "use threads" 引用命名空间,不能通过 , do, 或者 require。
  3. 如果有变量需要共享,必须引用 "threads::shared"。 并在定义变量的时候如下:

                      my $var1 : shared = "value";

  以下是一个简单的使用perl 多线程的例子。

 

perl <wbr>多线程

# !/ usr / local / bin / perl   
use threads;   

@domain   
=    ( " tom.com " ,    " chinadns.com " ,    " 163.com " ,    " aol.com "
);   
for  ($i = 0 ;$i < 4 ;$i ++
)
{   
    print   $i.
' . ' .$domain[$i]. '       '
;   
}   
print   
" \n "
;   
    
my   $thr0   
=    threads -> new (\ & checkwhois,    ' 0 ' ,   $domain[ 0
]);   
my   $thr1   
=    threads -> new (\ & checkwhois,    ' 1 ' ,   $domain[ 1
]);   
my   $thr2   
=    threads -> new (\ & checkwhois,    ' 2 ' ,   $domain[ 2
]);   
my   $thr3   
=    threads -> new (\ & checkwhois,    ' 3 ' ,   $domain[ 3
]);   
    
sub   checkwhois()   
{   
    my ($l,$r)
=
@_;   
    my $i
= 0
;   
    
while ($i < 1000000
)   
    {   
          $i
*
$i;   
          $i
++
;   
    }   
    print   
" done  --$l\t\n "
;   
    print   $l.$r.
"    query   successful!   \n "
;    
}

$thr0
->
join;  
$thr1
->
join;   
$thr2
->
join;   
$thr3
->
join;
这个简单的perl主要是新建了4个子线程去做不同的事情,然后调用join方法等待他们执行完成并让线程自动回收
但有时,还是需要结合folk 做一些复杂的工作,下面是关于这个的例外一个demo。看原文连接

例子三:
perl多线程编程给我的总体感觉有点像
Linux C POSIX 多线程编程。 perl 多线程编程使用起来非常方便,但要深入掌握还是要下点功夫的。这里有一个简单的例子:

 


#!/bin/perl
 
use strict;
use threads;
use Cwd;
use POSIX qw(strftime);
 
################################################################################
# 函数名:  count
# 函数描述:  数数
# 输入:   name 随意输入一个名字
# 输出:   无
# 调用:  
# 被调用: 
# 返回:
################################################################################
sub count
{
   my ($name) = @_;
   my $current_time = strftime "%Y-%m-%d %H:%M:%S", localtime;
   for ($i = 0; $i <= 10000; $i++)
   {
     print "$current_time  $name $i";
   }
}
 
创建第一批线程
my $thread_1_01 = threads->create('count', Thread_1);
my $thread_1_02 = threads->create('count', Thread_2);
my $thread_1_03 = threads->create('count', Thread_3);
my $thread_1_04 = threads->create('count', Thread_4);
 
# 等待第一批线程结束完成
$thread_1_01->join();
$thread_1_02->join();
$thread_1_03->join();
$thread_1_04->join();

# 创建第二批线程
my $thread_2_01 = threads->create('count', Thread_5);
my $thread_2_02 = threads->create('count', Thread_6);
my $thread_2_03 = threads->create('count', Thread_7);
 
# 等待第二批线程结束完成
$thread_2_01->join();
$thread_2_02->join();
$thread_2_03->join();
分享到:
评论
1 楼 luogen33 2011-12-02  
[/b][b]粗体: [b]文字 (alt+b)[/b]

相关推荐

    perl多线程教程集

    perl多线程教程集

    perl 多线程

    perl 多线程的简单讲解, 适合入门级别的。

    perl实现多线程详解[整理].pdf

    perl实现多线程详解[整理].pdf

    多线程以及对数组的互斥操作的演示程序

    从5.6版本开始Perl已经开始部分的支持多线程,而在5.8版本中进一步的得到了完善,不过目前国内对于Perl多线程编程方面的资料相对较少,由于工作上的原因可能会使用到多线程方面的东西所以写了一个测试程序,运行以后...

    mt-aws-glacier:将Perl多线程多部分同步到Amazon Glacier

    mt-aws-glacier:将Perl多线程多部分同步到Amazon Glacier

    PERL编程24学时教程

    PERL编程24学时教程, 全面通俗易懂。

    perl实现多线程详解.pdf

    perl实现多线程详解.pdf

    Perl多进程技术在自动化测试中的应用

    Perl的多进程自动化测试场景介绍应用Perl多进程技术到自动化测试脚本小结参考资料在IT产品系统测试的自动化项目中,经常有并行处理多个子任务的需求,为了提高测试效率,就需要用到多进程或者多线程编程。...

    Network Programming With Perl

    第11章 多线程应用程序 第12章 多路复用应用程序 第13章 无阻塞I/O 第14章 安全的服务器 第15章 预创建于进程和预创建线程 第16章 IO:: Poll 第4部分高级话题 第17章 TCP紧急数据 第18章 UDP协议 第19章...

    PERL线程池

    高效的线程池库,使用方便, 规避了PERL自身庞大的库载入速度慢的缺点

    Perl 6 技术参考手册.docx

    Perl,一种功能丰富的计算机程序语言,运行在超过100种计算机平台上,适用广泛,从大型机到便携设备,从快速原型创建到大...本文档是Perl语言的全部语法和Perl 6的开发实例集合,包括面向对象、文本处理和多线程等。

    PHP中多线程的两个实现方法

    当有人想要实现并发功能时,他们通常会想到用fork或者spawn threads,但是当他们发现php不支持多线程的时候,大概会转换思路去用一些不够好的语言,比如perl。 其实的是大多数情况下,你大可不必使

    Proxyp:多线程代理枚举实用程序-开源

    Proxyp是一个小的多线程Perl脚本,用于枚举等待时间,端口号,服务器名称以及代理IP地址的地理位置。 该脚本起初是一种加速使用代理链的方式,这就是为什么我添加了一个append选项,以便将生成的实时IP地址放置在...

    pWins Webserver-开源

    pWins 是一个纯粹的 perl 多线程 httpd,适用于小型家庭服务器和大型互联网公司。 网络分发、VHOST、日志数据库、perl、ruby、php……易于安装和配置。

    CMS-FAST-CHECKR:快速简单流畅的CMS CHECKER多线程〜

    快速简单流畅CMS CHECKER多线程〜您需要安装Parallel :: ForkManager 如何安装: 1- cpan并行:: ForkManager 2-移至Parallel-ForkManager目录3- perl Makefile.PL && make测试&& make install或perl Makefile.PL &&...

    Perl高级教程人称“大骆驼”(免费)

    第一章 Perl概述............................................................................................................................10 1.1 从头开始.................................................

    如何解决PHP无法实现多线程的问题

    当有人想要实现并发功能时,他们通常会想到用fork或者spawn threads,但是当他们发现PHP不支持多线程的时候,大概会转换思路去用一些不够好的语言,比如Perl。 假设你要建立一个服务来检查正在运行的n台服务器,以...

    prinseq_parallel:Prinseq的并行多线程多线程版本

    prinseq_parallel:Prinseq的并行多线程多线程版本

    php fsockopen中多线程问题的解决办法[翻译]

    回答: 当有人想要实现并发功能时,他们通常会想到用fork或者spawn threads,但是当他们发现php不支持多线程的时候,大概会转换思路去用一些不够好的语言,比如perl。 其实的是大多数情况下,你大可不必使用fork...

Global site tag (gtag.js) - Google Analytics