piyush.shourie has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am writing a perl script which will be run in a multiprocessing environment (basically using FORK method). However, since my script uses LWP module, and LWP module does not support multiprocessing, the script causes Perl to crash.

I am using Active Perl 5.6.

Please suggest a way out to handle the issue.
The code snippet to depict the problem is given below:

my $NumThreads = 10; print "Starting Test Cases in Threaded Environment:\n\n"; my $strRequestType = "GET"; my $strRequestUrl = "http://mohwks11232.ad.com/a.html"; my $objUserAgent = new LWP::UserAgent; my $objRequest = HTTP::Request->new($strRequestType,$strRequestUrl); # Create the requested number of threads of execution. for ($i = 1;$i <= $NumThreads; $i++) { spawnThread($i); } # Wait for all the child threads to finish and then go home. $child = 0; do { $child = wait(); } until $child == -1; print "\n Run completed successfully.\n"; # All done so go home! exit(0); sub GETResource() { print( "\nTesting BASIC (GET) authentication\n"); print( "----------------------------------"); my $objResponse = $objUserAgent->simple_request($objRequest); # This thread is all done. exit; } sub spawnThread { FORK: { if($pid = fork) { # parent process...don't do anything } elsif(defined $pid) { # We are a child thread so go do some work. GETResource(); exit; } elsif($! == EAGAIN) { #EAGAIN is the supposedly recoverable fork error sleep 5; redo FORK; } else { # unrecoverable fork error die "Fatal Error: Can't fork: $!\n"; } } }

Replies are listed 'Best First'.
Re: how to make LWP calls compatible with forked script
by iburrell (Chaplain) on Sep 02, 2004 at 16:47 UTC
    First, you aren't using threads, you are using multiple processes.

    Second, why don't you create an LWP::UserAgent in each child process. Sharing the object between the processes does not share the data. It may create problems with sharing a socket or similar. Creating the object should be cheap.

      Hi,

      Firstly, I have already updated the problem description.
      Secondly, I tried creating LWP::UserAgent in each child process, but of no use. Still it crashes the Perl.
      Can you please suggest a way to handle this?

      Thanks in advance

        It might be a bug in ActivePerl. Perl on Windows does some special stuff to provide the Unix API on Windows which does things differently. For example, fork() is emulated with threads, and sockets need to be mapped to WinSock API.

        I know there were some bugs in earlier releases of ActivePerl 5.6. Make sure you have the most recent release. ActivePerl 5.8 is supposed to be better; try that if you can. Finally, what Windows version are you running? The NT-based ones (NT 4, 2000, XP) are more stable than 95, 98, ME.

Re: how to make LWP calls thread-safe
by Anonymous Monk on Sep 02, 2004 at 13:26 UTC
    FYI, you're not using threads
      If no threads, can't make thread-safe