LWP::UserAgent & threads problem

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";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top