OK so I didn't understand as much as I had hoped, and tried to write something much simpler. Even that didn't work, and I'm not sure what I'm doing wrong. I figured I would remove the whole 'terminating a process' part from the equation and try to add that in later and just try to make a threaded ping script

use strict; use threads; use threads::shared; use Thread::Queue; use Net::Ping; use constant NUMBER_OF_THREADS => 20; use constant TIMEOUT => 4; use constant INPUT_FILE_NAME => "slist.txt"; our %results : shared; sub pingworker { my ($Qin, $timeout) = @_; my $response; while (my $server = $Qin->dequeue) { $response = Net::Ping->new()->ping($server); lock(%results); $results{$server} = $response ? "Alive" : "Timed Out"; } } my $Qwork = Thread::Queue->new; my $thread; for (1 .. NUMBER_OF_THREADS) { $thread = threads->new(\&pingworker, $Qwork, TIMEOUT); $thread->detach; } open(SLIST, INPUT_FILE_NAME) || die "Cannot open server list: $!\n"; while (<SLIST>) { chomp; $Qwork->enqueue($_) if /[A-Za-z0-9\.\-_]+/; sleep 0.5 while $Qwork->pending > NUMBER_OF_THREADS; } close(SLIST); $Qwork->enqueue( (undef) x NUMBER_OF_THREADS); sleep 0.5 while $Qwork->pending > 0; foreach my $server (sort keys %results) { print $server . "," . $results{$server} . "\n"; }

So this actually seems to work (and I can understand the syntax) except for the fact that the results are completely wrong (its reporting most of the targets as non-responsive when they are). When i've tried to do output from the threads by locking $semSTDOUT it appears that the first time through it gets a real response from ping and after that or (seemingly) sometimes the second time through it starts reporting everything as nonresponsive.

I still don't understand the purpose of the output queue (as opposed to just stuffing it into a big array or something) in the original example, but that doesn't seem to be my big problem. I would guess thats for the case where the results need to be output while the script is running (e.g. if it were too large to store in memory?)


In reply to Re^10: Win32::Process output by tawnos
in thread Win32::Process output by tawnos

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.