如何在Perl中使用线程? [英] How to use threads in Perl?
本文介绍了如何在Perl中使用线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Perl中使用线程来提高程序速度……例如,我想在此代码中使用20个线程:
I want to use threads in Perl to increase the speed of my program ... for example i want to use 20 threads in this code:
use IO::Socket;
my $in_file2 = 'rang.txt';
open DAT,$in_file2;
my @ip=<DAT>;
close DAT;
chomp(@ip);
foreach my $ip(@ip)
{
$host = IO::Socket::INET->new(
PeerAddr => $ip,
PeerPort => 80,
proto => 'tcp',
Timeout=> 1
)
and open(OUT, ">>port.txt");
print OUT $ip."\n";
close(OUT);
}
在上面的代码中,我们给出了ip列表并扫描了给定的端口.我想在此代码中使用线程.还有其他方法可以提高我的代码速度吗?
In the above code we give a list of ips and scan a given port. I want use threads in this code. Is there any other way to increase the speed of my code?
谢谢.
推荐答案
您可能不希望使用线程,而是要研究 AnyEvent :: Socket 或 Coro :: Socket 或 POE 或
Instead of using threads, you might want to look into AnyEvent::Socket, or Coro::Socket, or POE, or Parallel::ForkManager.
这篇关于如何在Perl中使用线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文