The following does similarly to what the OP described. I've experienced lost traps on Linux, causing the server script to never leave the loop. On the Mac, it takes 3.822 seconds to process 10,000 traps when successful.

use strict; use warnings; use threads; use threads::shared; use Time::HiRes qw( sleep time ); use Net::SNMPTrapd; use MCE::Flow; use MCE::Queue; my $tid = 0; sub CLONE { $tid = threads->tid(); } my $snmptrapd = Net::SNMPTrapd->new( ReusePort => 1 ); my $max_workers = 30; my $count : shared = 0; my $start : shared; MCE::Flow::init { user_output => sub { print {*STDOUT} $_[0]; }, user_error => sub { print {*STDERR} $_[0]; }, }; mce_flow { max_workers => $max_workers }, \&server; printf {*STDERR} "duration: %0.03f seconds\n", time - $start; exit(0); sub server { my $done = 0; while ( 1 ) { my $trap; { lock $count; $start = time unless $start; $trap = $snmptrapd->get_trap(); $done = 1 if ( ++$count > 10000 - $max_workers ); } if ( !defined $trap ) { MCE->printf(\*STDERR, "$0: %s\n", Net::SNMPTrapd->error()) +; next; } elsif ( $trap == 0 ) { next; } $trap->process_trap(); MCE->printf( "[%02d] %s\t%i\t%i\t%s\n", $tid, $trap->remoteaddr, $trap->remoteport, $trap->version, $trap->community ); last if $done; sleep 0.004; } }

Regards, Mario.


In reply to Re^3: worker threads - one does all the work by marioroy
in thread worker threads - one does all the work by jvuman

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.