U
unreal
Hi,
I have problem using LWP::UserAgent with threads.
In function get_google_page() I create object from class
LWP::UserAgent and get page 'http://google.com'. After this create 3
threads and 1 object from class LWP:UserAgent in every thread. But
there is error 'Segmentation fault' before creating threads. All
objects are declared as my and I don't know why there is 'Segmentation
fault'.
If I don't call function get_google_page() (i.e. don't create object
before creating threads) there aren't problem.
Please look at code below. Can someone help me?
use threads;
use threads::shared;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use LWP::UserAgent;
get_google_page(); # if this row is in comment there isn't problem
for ( my $i = 0; $i < 3; $i++ )
{
$threads[$i] = threads->create("follow_URL", $i);
}
for ( my $i = 0; $i < 3; $i++ )
{
$threads[$i]->join();
}
sub follow_URL
{
my $TID = shift;
print "\n TID: $TID started";
my $user_agent = LWP::UserAgent->new;
$user_agent->protocols_allowed( [ 'http', 'https'] );
$user_agent->agent("Mozilla/8.0");
$user_agent->timeout( 30 );
print "\n TID: $TID ended";
}
sub get_google_page
{
my $ua = LWP::UserAgent->new;
$ua->protocols_allowed( [ 'http', 'https'] );
$ua->agent("Mozilla/8.0");
$ua->timeout( 30 );
my $request = HTTP::Request->new(GET => 'http://google.com');
$request->header('Accept' => 'text/html');
$res = $ua->request($request);
if ( !$res->is_success )
{
print "Unable to get google page!\n";
return 0;
}
print "Successfully get google page!\n";
}
I have problem using LWP::UserAgent with threads.
In function get_google_page() I create object from class
LWP::UserAgent and get page 'http://google.com'. After this create 3
threads and 1 object from class LWP:UserAgent in every thread. But
there is error 'Segmentation fault' before creating threads. All
objects are declared as my and I don't know why there is 'Segmentation
fault'.
If I don't call function get_google_page() (i.e. don't create object
before creating threads) there aren't problem.
Please look at code below. Can someone help me?
use threads;
use threads::shared;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use LWP::UserAgent;
get_google_page(); # if this row is in comment there isn't problem
for ( my $i = 0; $i < 3; $i++ )
{
$threads[$i] = threads->create("follow_URL", $i);
}
for ( my $i = 0; $i < 3; $i++ )
{
$threads[$i]->join();
}
sub follow_URL
{
my $TID = shift;
print "\n TID: $TID started";
my $user_agent = LWP::UserAgent->new;
$user_agent->protocols_allowed( [ 'http', 'https'] );
$user_agent->agent("Mozilla/8.0");
$user_agent->timeout( 30 );
print "\n TID: $TID ended";
}
sub get_google_page
{
my $ua = LWP::UserAgent->new;
$ua->protocols_allowed( [ 'http', 'https'] );
$ua->agent("Mozilla/8.0");
$ua->timeout( 30 );
my $request = HTTP::Request->new(GET => 'http://google.com');
$request->header('Accept' => 'text/html');
$res = $ua->request($request);
if ( !$res->is_success )
{
print "Unable to get google page!\n";
return 0;
}
print "Successfully get google page!\n";
}