我使用以下子程序运行大约40个线程:
my $app = shift;
my $ua = LWP::UserAgent->new();
$ua->timeout(5);
my $response = $ua->get($$app{'watch_url'});
my $new_md5;
if ($response->is_success()) {
$new_md5 = md5_hex($response->content());
}
return ($$app{'short_name'}, $$app{'watch_md5'}, $new_md5);
核心转储在大约3/4的时间内发生。LWP和LWP::UserAgent是纯Perl的,因此我对此措手不及。LWP::UserAgent不线程安全吗?
更新:
下面是一个最简单的版本来重现这个问题:
use strict;
use warnings;
use threads;
use LWP::UserAgent;
sub check_app {
my $ua = LWP::UserAgent->new();
$ua->timeout(5);
$ua->get('http://www.flatdoc.com/?' . rand(10));
}
my @threads;
for (my $i = 0; $i < 40; $i++) {
my $thread = threads->create(\&check_app);
push(@threads, $thread);
}
foreach (@threads) {
$_->join();
}
发布于 2009-12-05 09:16:15
非线程安全的纯Perl代码不会导致分段错误(实际上,没有任何纯Perl代码会导致分段错误)。Perl中的一个bug会导致分段错误。Perl中的线程在历史上是非常错误的,但是它们已经变得更好了。
您的代码在5.10.1中运行良好,HTTP::Lite可能只是不挠您遇到的perl bug。很可能您只需要使用一个较新版本的Perl。越老越接近Redhat,线程就越不稳定。如果要使用线程,可以使用最新的Perl。
作为线程的替代,您可以使用类似并行::ForkManager、LWP::并行或甚至惊人的叉子模块来模拟使用叉的线程。
https://stackoverflow.com/questions/1843932
复制相似问题