in reply to Re^6: Should I use threads? Perl/DHCP/Radius
in thread Should I use threads? Perl/DHCP/Radius
This is the newest based on your last post with an async block monitoring joinable threads. Noticed a significant change. LAst thread to be launched finished in 4 seconds instead of 8.#!/usr/bin/perl use strict; use LWP::UserAgent; use HTTP::Request; use threads; use threads::shared; use strict; use Time::HiRes qw( gettimeofday tv_interval); use IO::Handle; STDOUT->autoflush(1); # Reg Exp Patterns my $rexphour = '[0-2][0-9]'; my $rexpminsec = '[0-5][0-9]'; my $rexpmac = '([A-Fa-f0-9]{2}:{0,1}){6}'; my $rexpip = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; my $rexppcname = '\(.*?\)'; my $rexpethIface = 'via\s+.*?'; my $rexpPattern = qr/.*?($rexphour):($rexpminsec):($rexpminsec).*?($re +xpip).*?($rexpmac)\s($rexppcname){0,1}.*?($rexpethIface)$/; my $stext = "dhcpd: DHCPACK on "; my %all_threads; my ($line, $cmac, $pcname, $NWIface, $ip); my @joinable; my %clients :shared; my @threads; open(DETAIL,"<dhcpleases") or die "ERROR messages "; seek(DETAIL,1,2); for (;;){ while (<DETAIL>) { chomp; $line = $_; # Get current time my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = loc +altime(time); # Search for DHCP Acks and Parse Time, MAC, IP, Device Name, NW Iface if($line =~ /$stext.*/){ $line =~ /$rexpPattern/; my $secepoch = timelocal($3,$2,$1,$mday,$mon,$year, $wday, + $yday, $isdst); $ip = $4; $cmac = uc($5); $pcname = $7 ; if($pcname eq ""){ $pcname = "?"; } $NWIface = $8; }# if $all_threads{$cmac} = threads->create ( \&checkmacapi, $cmac ); }#while seek(DETAIL, 0, 1); my $finisher = async{ while (1) { foreach my $macthread (threads->list(threads::joinable)) +{ $macthread->join(); } }#while }; }# sub checkmacapi{ my $mac = shift; my $macAuthURL = "http://internaladdressconfidential"; my $t0; my $t1; my $tint; my ($request, $ua, $response, $respcon, $rval); my ($returnkey,$returnval); print "Thread ".threads->tid()." Launched \n"; # print usecTime()." Thread".threads->tid(). ": MAC: ".$mac." Cont +acting $macAuthURL$mac.\n"; $t0 = [gettimeofday]; # Wrap call to API with eval eval { local $SIG{ALRM} = sub { die "Alarm\n" }; alarm(16); my $macr = $macAuthURL.$mac; $request = HTTP::Request->new(GET => $macr); $ua = LWP::UserAgent->new(); $response = $ua->request($request); $respcon = $response->content(); alarm(0); }; # We check the response object's status if($response->status_line =~ /^500/ ){ print "MAC: ".$mac." Network Delay/Error."; { lock (%clients); $clients{$mac} = 0; } } # print usecTime()." Thread".threads->tid().": MAC: ".$mac." Server + response: $respcon \n\n\n" ; # All OK - Ensure that sent mac is returned back on success. if( $response->{_rc} == 200 ){ $response->{_content} =~ s/[\{\} \"]//g; ( $returnkey, $returnval ) = split(/:/,$response->{_content},2 +); print "MAC: ".$mac." returned with status: $response->{_rc} " +; if( $mac eq uc($returnval) ){ { lock (%clients); $clients{$mac} = 1; } } else{ # Malformed URL { lock (%clients); $clients{$mac} = 0; } } } else{ { lock (%clients); $clients{$mac} = 0; } } $t1 = [gettimeofday]; $tint = tv_interval $t0, $t1; print "Thread ".threads->tid()." time :$tint\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Should I use threads? Perl/DHCP/Radius
by BrowserUk (Patriarch) on Aug 25, 2010 at 16:21 UTC | |
by MonkeyMonk (Sexton) on Aug 26, 2010 at 08:53 UTC | |
by BrowserUk (Patriarch) on Aug 26, 2010 at 10:07 UTC | |
by zentara (Cardinal) on Aug 26, 2010 at 11:20 UTC |