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


In reply to Just can't get threads to work by stellagoddc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.