I dont want to have a thread per device constantly running, especially if all its doing is sleeping.
Why not?
This would be how I would do it. Note, I've no way to test the SNMP stuff, so I've just cleaned up the code you posted elsewhere.
#! perl use strict; use threads; use threads::shared; use Net::SNMP; sub polldevice { my( $running, $stop, $host, $community, $delay ) = @_; ++$running; my( $session, $error ) = Net::SNMP->session( Hostname => $host, Community => $community ) or die "session error:" . $session->error; do { my $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" ) or die "request error: " . $session->error; print "Testing ".$host."\n"; 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"; } while not $stop and sleep $delay; $session->close; --$running; } my $stop :shared = 0; my $running :shared = 0; while( <DATA> ) { async{ \&polldevice, $stop, split ' ' }->detach; } $SIG{INT} = sub{ $stop = 1 }; sleep 1 until $stop; sleep 1 while $running; __DATA__ 192.168.1.50 public 10 192.168.1.1 public 25 192.168.1.2 public 60 192.168.1.3 public 45 192.168.1.4 public 12 192.168.1.5 public 59 192.168.1.6 public 01
In reply to Re: Running subroutines at specific times
by BrowserUk
in thread Running subroutines at specific times
by stellagoddc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |