Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Program Design Around Threads

by BrowserUk (Pope)
on Mar 06, 2013 at 18:48 UTC ( #1022064=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Program Design Around Threads
in thread Program Design Around Threads

Sorry I wasn't clear enough here. I want one file per queried machine with all of the command outputs in it.

Then I do not understand your stated problem (from the OP): "The problem I am trying to solve is the printed order of the commands in the output file. ", or why you think you need all those darn semaphores in your code?

If you programmed a single threaded solution to this is might look something like:

for my $machine ( @machines ) { open my $out, '>', "$machine.dat" or die $!; for my $cmd ( @commands ) { my $content = get "$machine/$cmd"; print $out $content; } close $out; }

The outputs from the commands end up in the file in the same order as the commands are run, because that the order you print them in.

To turn that into a threaded solution, just make the body of the outer loop the thread:

for my $machine ( @machines ) { async { open my $out, '>', "$machine.dat" or die $!; for my $cmd ( @commands ) { my $content = get "$machine/$cmd"; print $out $content; } close $out; }->detach; }

And (essentially*) that's it! Each thread it using a different file, so no conflicts or ordering problems arise. No need for locking or semaphores or synchronisation.

*As shown, the above would start a new thread for every one of the 1000s of machines and run them concurrently which would blow your memory to hell and thrash your disc to death. But fixing that is very simple:

my $running :shared = 0; ## This tracks the number of conc +urrent threads for my $machine ( @machines ) { async { { lock $running; ++$running; } ## incr on start open my $out, '>', "$machine.dat" or die $!; for my $cmd ( @commands ) { my $content = get "$machine/$cmd"; print $out $content; } close $out; { lock $running; --$running; } ## decr on finish }->detach; sleep 1 until $running < 10; ## sleep a bit if more + than 10 are running } sleep 1 while $running; ## Make sure the main threads waits for th +e last few threads to finish

(That would be simpler still if the API allowed sleep 1 while threads->list( threads::detach ) > 10;; but it doesn't.)

It would also be more efficient of your machine resources (cpu & memory) to use a thread pool (NOT Thread::Pool!!!) solution; but as you're IO-bound; and limiting that for the sake of your proxy; you are unlikely to trouble the resources of even the least well specified machine with the above code.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.


Comment on Re^3: Program Design Around Threads
Select or Download Code
Re^4: Program Design Around Threads
by aeaton1843 (Acolyte) on Mar 06, 2013 at 21:21 UTC

    That's just it though. This isn't true: "Each thread it using a different file, so no conflicts or ordering problems arise. No need for locking or semaphores or synchronisation." There are as many threads running as there are machines and commands. For Example:

    tid1 -> machine1 -> "show running-config" takes 30 seconds to get reply.

    tid2 -> machine2 -> "show running-config" takes 30 seconds to get reply

    tid3 -> machine1 -> "show vlan" takes 5 seconds to get reply.

    tid4 -> machine2 -> "show vlan" takes 5 seconds to get reply.

    Since tid 3&4 finish first, my 2 output files now have show vlan at the top instead of show running-config because those threads finished before thread 1&2 and wrote contents into file machine1.txt and machine2.txt. Maybe I am missing something?

      There are as many threads running as there are machines and commands

      Only if you run the different commands for each machine from different threads. That's the wrong way to do it.

      If you look at the code snippets I posted, one thread connect to one machine, opens one file, and runs all the commands for that machine, sequentially. That way, there is no possibility for ordering problems or file conflicts.

      Maybe I am missing something?

      Please read my last post slowly and thoroughly. Making multiple connections to each machine from different threads to run single commands is both inefficient and the source of your problems.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

        Please don't take this the wrong way. You are hitting me with the clue stick and I want to make sure I understand everything here.

        As I understand your code, it gives each machine in the machines list a thread and then runs through each of the commands in the commands list sequentially.

        What I don't understand is why your statement: "Making multiple connections to each machine from different threads to run single commands is both inefficient and the source of your problems." is true. Except for the last part because we both agree there. When I look at it, let's say I have three commands per device each of which take on average 15 seconds to retrieve. If I create a thread for each command, I wait 15 seconds to finish all three commands for that machine. That is, of course, given a high enough allowed running thread count. If I do it your way I no longer have the synch issues but I am now waiting an extra 30 seconds to finish each machine. Granted you have 10 machines threads running sequentially.

        We agree about the source of my problems. I am trying to further wrap my head around why saving a possible 30 seconds per device in this scenario was a less than optimal approach. That is other than the fact that it causes me a lot of synchronization issues. So be it if that is the answer. At least I found out the right way to go about the problem even if it takes a bit longer to get all the output.

        I do appreciate the comments.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-08-21 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (135 votes), past polls