For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2004 > LWP::UserAgent & threads problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author LWP::UserAgent & threads problem
unreal

2004-04-30, 11:19 am

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







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com