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

Hi Monks,

I've been 'dabbling' with trying to write a threaded perl script. I thought I would try with something simple, just to get the ball rolling, or what I thought would be simple, please see the code below.

#!/usr/bin/perl use Net::SNMP; use threads; use Config; $Config{usethreads} or die "Recompile Perl with threads to run this p +rogram."; threads->new(\&polldevice,"192.168.1.50", "public"); threads->new(\&polldevice,"192.168.1.1", "public"); sub polldevice { my ($host, $community) = @_; ($session,$error) = Net::SNMP->session(Hostname => $host, Community => $community); die "session error: $error" unless ($session); $result = $session->get_request("1.3.6.1.2.1.1.2.0", "1.3.6.1.2.1.2.1. +0", "1.3.6.1.2.1.1.3.0"); die "request error: ".$session->error unless (defined $result); $session->close; print "Testing ".$host."\n"; sleep 10; print "Number of interfaces: ".$result->{"1.3.6.1.2.1.2.1.0"}."\n"; print "uptime: ".$result->{"1.3.6.1.2.1.1.3.0"}."\n"; print "sysOID: ".$result->{"1.3.6.1.2.1.1.2.0"}."\n"; }

The hope was that both devices would be polled at the same time, I was using the 'sleep 10' to try and proove this. The Code works fine when I pass in the polling details 1 by 1. When I run it all I get is,

A thread exited while 3 threads were running.

And nothing else gets printed to the window. I've even tried doing it in a 'use Threads' style, but keep seeming to get the same problem.

I'm using 5.8.8 on SUSE Linux 10.1 RC1.

I would be extremely grateful for any guidance

Thanks

Dave

Replies are listed 'Best First'.
Re: Just can't get threads to work
by BrowserUk (Patriarch) on Apr 20, 2006 at 09:59 UTC

    Adding the following line after you've started your threadsm would prevent the main thread from terminating before the threads have finished and allow your code to work. You can also do away with the sleep as it is just slowing things up.

    $_->join for threads->list;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks that kind off worked

      Unfortunately I found myself running out of memory, when I really pushed the threads. putting ->join at the end of each new thread seemed to fix teh memory problem

      Thanks again

Re: Just can't get threads to work
by Corion (Patriarch) on Apr 20, 2006 at 08:58 UTC

    While I have stayed away from threads in Perl, usually, you need to wait for your threads to finish before you exit with your main thread. Maybe detaching the threads also keeps the main program alive. I'd try the following for kickstarting the threads:

    my @threads; my @ips = qw( 192.168.1.50 192.168.1.1 ); for my $ip (@ips) { push @threads, threads->new(\&polldevice,$ip, "public"); }; # Now, wait for all threads to finish: for my $thread (@threads) { print "Waiting for thread $thread to finish.\n"; my @results = $thread->join(); print "(got @results)\n"; }; print "All threads finished.\n";
      Ofcourse, the main script ends, killing the threads, doooh.

      My final script will always be running, which is why I wasn't thinking about that.

      Thanks again.