in reply to Re^9: Win32::Process output
in thread Win32::Process output

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?)