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

In reply to how to make LWP calls compatible with forked script by piyush.shourie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.