Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Forking Clients

by gepapa (Acolyte)
on Oct 15, 2008 at 17:27 UTC ( [id://717281]=note: print w/replies, xml ) Need Help??


in reply to Re: Forking Clients
in thread Forking Clients

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

Replies are listed 'Best First'.
Re^3: Forking Clients
by BrowserUk (Patriarch) on Oct 15, 2008 at 17:32 UTC

    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.

        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?

        No. What you are describing probably requires a completely different architecture to your OP code. But you first need to ask yourself a few questions.

        The first problem is how are you ever going to get to call blee() once you've called blah()?

        Ie. In the code that calls blah() and blee(), as you've described it in this post, once you call the first of these, it is going to block until it gets it's results, so you won't be able to call the second until the first has finished.

        So how can "These function may very well call rpc at the same time," be so?

        Once you decide how the top level of your code is going to operate, then you can decide how best to structure it?


        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://717281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found