in reply to Forking Clients

If you don't have an irrational fear of threads, something like this complicated beast would do it :):

#! perl -slw use strict; use threads; use threads::shared; use Thread::Queue; use ClarRPC; #In house module my $Q = new Thread::Queue; my $ip = '10.15.51.208'; my $command = 'ping -n 15 10.15.51.208'; my @ports = 1300 .. 1302; for my $port ( @ports ) { async{ my $connection = ClarRPC->connect( $ip, $port ); my @results :shared = $connection->rpc( 'ClarRPCService::system_call', $command ); $connection->disconnect(); unshift @results, $port; $Q->enqueue( \@results, undef ); }->detach; } for ( 1 .. @ports ) { while( my $ref = $Q->dequeue ) { my( $port, @results ) = @$ref; print "$port : @results"; } } __END__ c:\test>junk4 1300 : 1 2 3 4 5 6 7 8 9 10 1301 : 1 2 3 4 5 6 7 8 9 10 1302 : 1 2 3 4 5 6 7 8 9 10

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Forking Clients
by gepapa (Acolyte) on Oct 15, 2008 at 17:27 UTC

    Hi first, thanks for your reply, but I have a few questions.

    use strict; use threads; use Thread::Queue; use ClarRPC; my $queue = new Thread::Queue; rpc('10.15.51.208', '1300', 'ping -n 50 10.15.51.208'); rpc('10.15.51.208', '1301', 'ping -n 10 10.15.51.208'); rpc('10.15.51.208', '1302', 'ping -n 50 10.15.51.208'); while (my $ref = $queue->dequeue) { my ($port, @results) = @$ref; print "$port : @results\n"; } sub rpc { my ($ip, $port, $command) = @_; async{ my $connection = ClarRPC->connect($ip, $port); my @resp :shared = $connection->rpc('ClarRPCService::system_ca +ll', $command); $connection->disconnect(); unshift(@resp, $port); $queue->enqueue(\@resp, undef); }->detach }

    So in this case note the 1301 port command is a -n 10, so this one will return prior to the others. So what I see in this instance is that the program exits, as soon as the 1301 port returns its info. What I was hoping for was for something that wouldn't exit until they were all done. I don't see anything glaring that I missed in my version which would make it different than yours in functionality.

    Any ideas? Thanks

      You need to wrap the while loop in a for loop that iterates once for each client you start:

      use strict; use threads; use threads::shared; ### I omitted this from my example above initiall +y. use Thread::Queue; use ClarRPC; my $queue = new Thread::Queue; rpc('10.15.51.208', '1300', 'ping -n 50 10.15.51.208'); rpc('10.15.51.208', '1301', 'ping -n 10 10.15.51.208'); rpc('10.15.51.208', '1302', 'ping -n 50 10.15.51.208'); for( 1 .. 3 ) { ## Must iterate once for each thread started ### while (my $ref = $queue->dequeue) { my ($port, @results) = @$ref; print "$port : @results\n"; } } sub rpc { my ($ip, $port, $command) = @_; async{ my $connection = ClarRPC->connect($ip, $port); my @resp :shared = $connection->rpc( 'ClarRPCService::system_call', $command ); $connection->disconnect(); unshift(@resp, $port); $queue->enqueue(\@resp, undef); }->detach }

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Yes, sorry for overlooking that. I have a few general questions more about understanding and extending this concept.

        This is a rather simple proof of concept, the final implementation would involve the rpc function being called from multiple different places, possibly simultaneously, in this case I want the caller to wait until its response is ready, when it is it should grab it and go on, while the other callers continue to wait.

        In Pseudo code it would be some thing like this:

        function rpc do thread and queueing stuff in here end rpc function blah rpc(IP, port2, command) look at queue see if your result is there if it is not wait else do other stuff with the info end blah function blee rpc(IP, port1, command) look at queue see if your result is there if it is not wait else do other stuff with the info end blah

        These function may very well call rpc at the same time, they will always use different port numbers. How would I go about using the queue to determine if my information is back, since in each instance of the call I won't know how many ports I have open, it only knows about the port it was told to use. Should I dequeue check the port/result and if it doesn't match enqueue it again?

        I really appreciate all your help.