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

Thread Advice

by crackotter (Beadle)
on Jun 12, 2003 at 17:38 UTC ( [id://265420]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I am currently working on a project where I need to use SNMP to upgrade 3000 to 10,000 ethernet devices(firmware). I am rewriting a previous script I had written. The devices are stored in a hash as follows:
key = Mac address , value = IP address
Should I be using Threads for this?? What module - Thread::Pool??? I am developing this in a linux enviroment but will be moving to Solaris once done.
Any pointers would be very helpful..
Also, as a side note, I am using Net::SNMP. Is there another SNMP module somebody would recommened over Net::SNMP??

Replies are listed 'Best First'.
Re: Thread Advice
by BrowserUk (Patriarch) on Jun 12, 2003 at 19:13 UTC

    This sounds like a perfect application for threading, lots of IO wait states and (I assume) little or no interaction required between the threads.

    I would also concur that a thread pool is the right mechanism, though that's not ane endorsement of Thread::Pool if in fact such a module exists, as I (obviously) have never tried it.

    The type of model I would use for this goes something like the following (untested, pseudo-code).

    #! perl -w use strict; require 5.008; use threads; use Thread::Queue; # Adjust (slowly) till you find the optimum use constant MAX_THREADS => 10; my $dispatchQ = Thread::Queue->new(); my $resultsQ = Thread::Queue->new(); sub worker { # While there's stuff to be done while( $dispatchQ->pending() ) { # Get the next serverb my $server = $dispatchQ->dequeue(); # creat a new connection (A guess!!) my $smtp = Net::SMTP->new( $server ); # do whatever is required my $result = 'Whatever'; # Tack the server name on the front of the results # for later identification. The normal perl idiom of # using an (anonymous) array has the problem that it # causes rapid memory leaks. Using this simplistic # approach seems to be most reliable and scalable. $resultsQ->enqueue( join( $; , $server, $result ) ); } return; # When there is nothing left to do, die. } # Create the pool my @threads = map{ threads->create( \&worker ) } 1 .. MAX_THREADS; # Read the list of servers my @servers = do{ local (*ARGV, $/) = 'servers.list'; <> }; # Put the list on the queue $dispatchQ->enqueue( @servers ); # While there is still work to be done while( $dispatchQ->pending() ) { # Get the results as they become available my ($server, $result) = split $;, $resultsQ->dequeue(); # Record the results somewhere. } # DispatchQ is empty, each thread will terminate once it completes its + last task $_->join for @threads; # Wait till they finish # Record the last few results while( my ($server, $result) = @{ $resultsQ->dequeue } ) { # Record the results somewhere. } __END__

    This is extremely speculative code, but derived from some stuff I am playing with that seems to work quite well. I'd love some feedback (either way) if you use it.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Thread Advice
by perrin (Chancellor) on Jun 12, 2003 at 17:59 UTC
    You should use a multi-process model unless there is some reason it won't work for your problem. Multi-process is well understood and widely used. Threads are the new kid on the block. Stay away unless you need them or want to play with new toys (and have the leisure to do it).
      By "Multi-process" do you mean forking??
        Yes. There's a good example of doing this with shared data in merlyn's column.
Re: Thread Advice
by neilwatson (Priest) on Jun 12, 2003 at 18:28 UTC
    The Parallel::ForkManager may be of use to you.
    use Paralle::ForkManager; my $pm = new Parallel::ForkManager(x); # where x = number of forks foreach ($item) @items){ $pm->start and next; #do stuff in parallel here $pm->finish; } $pm->wait_all_children;
    Be very conservative on the number of forks until you are sure the server can handle it. I learned the hard way :)

    Neil Watson
    watson-wilson.ca

Re: Thread Advice
by hardburn (Abbot) on Jun 12, 2003 at 17:44 UTC

    How often are you planning on upgrading? If this is a one-time thing, don't get yourself buiried in the complexities of threading. If this is something that might happen once or twice a month, and if it takes longer than a single night to upgrade everything, then you can probably get a speed boost with parrellizaing. Alternatively, you can make one program that can take a portion of the list of hosts and run it multiple times, each run with a different section of the list.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I am planning on running the upgrade once a week. In mutiple locations. I have not used parrellizaing before. How do I do it? :)
      My current script uses thread pool, but I notice a occasional problem, thats why I was asking about the recommend style for this type of project.

        You're already doing parrellization. Any program that has portions running together is using parrellization. Though it isn't true "parrellisim" unless you're on a multi-processor machine, a cluster, or one of those new-fangled hyperthreading processors, but that's a pesky detail that I'm ignoring for the moment.

        Recommended style here is "whatever works for you" :)

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-23 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found