Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I am trying to use SOAP::Lite to test our server under load. I need HTTP keep_alive capability so that all the request are made over the same connection.
The design I have in mind is to fork every service request. The pseudo code is something like this...

use SOAP::Lite; my $soap_client = SOAP::Lite->uri('myuri') ->proxy('http://serviceendpoint', timeout => 30, keep_alive => 1); for (1 .. 10) { if(fork()) { #--- do something in parent #-- like wait for time specified by load rate etc. sleep(1); #--- just for pseudo code } else { $soap_client->call('SomeMethod',$paramter); } }

With the above what I am observing is that the SOAP client object in the forked child process closes the TCP connection after it gets the response from the server. I think this is because of the use of fork and the copy of the object going away after the child process is done. Is there a way around this so that the soap client uses the same connection (transport) across multiple forked processes?
Thanks. DP.

p.s.: I tried a bit to play around with IPC::Shareable but get some cryptic error Can't store CODE items at blib/lib/Storable.pm

p.p.s: In reality I am achieving the forking using POE and POE::Wheel::Run. The issue is the same as with the use of basic fork as above.

Replies are listed 'Best First'.
Re: SOAP::Lite How to use persistent connection across fork process
by zentara (Cardinal) on Jul 10, 2012 at 16:31 UTC
    Threads is perfect for this. You could create 1 Soap::Lite object and keep using it over and over. This is untested, but is simple to understand what is happening. I would use Glib or AnyEvent to make a timer to wake the thread, but this while loop shows the simplicity.
    #!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my $tgo:shared = 0;; my $tdie:shared = 0; my $thread = threads->new(\&check_it); # setup a timer or use a while loop here # probably need to Control-C out of this loop # unless you add some keyboard checking for a q # or whatever while(1){ sleep 10; $tgo = 1; print "starting check\n"; } $tdie = 1; $thread->join; print "done press any key to exit\n"; <>; ################################################### sub check_it { use SOAP::Lite; $|++; # this will be persistent in the thread my $soap_client = SOAP::Lite->uri('myuri') ->proxy('http://serviceendpoint', timeout => 30, keep_alive => 1); while(1){ if($tdie == 1){ return }; if ( $tgo == 1 ){ print "soap start\n"; $soap_client->call('SomeMethod',$paramter); print "soap done\n"; $tgo = 0; #turn off self before returning to sleep }else { sleep 1 } } } __END__

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh