in reply to Win32::Process output

See 633845

Replies are listed 'Best First'.
Re^2: Win32::Process output
by tawnos (Novice) on Nov 03, 2010 at 19:49 UTC

    OK so the syntax on that one is far beyond me. The idea though is something like this?

    use threads; $thread = $threads->new(\&dostuff, "options_here"); $thread->detach; sub dostuff { $return = `foo.exe -$_[0]`; print $return; do_logging_stuff($return); }

    I would need to limit the number of threads somehow though? No clue how to do this. And while I don't really care much if there are a few threads running about not completing, is there a (syntactically simple) method to kill threads that live longer than a certain period of time?

      Try this (from a couple of posts down from the link I provided at:633867):

      #! perl -slw use strict; sub timedCommand { use threads; use threads::shared; my( $cmd, $timeout ) = @_; my @results :shared; my $pid :shared; async { $pid = open my $fh, "$cmd |" or die "$!, $^E"; @results = <$fh>; }->detach; kill 0, $pid while sleep 1 and $timeout--; kill 3, $pid and return if $timeout; return @results; } ... ## Run foo.exe, kill it after 20 seconds. my @results = timedCommand( 'foo.exe -opt', 20 ); ...

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I think I (somewhat) understand now. The async {} is some sort of inline function definition? I could do the same thing by separating out that part into a sub and then calling the threads->new() function with a reference to the sub?

        Since this uses threading anyway, would there be a way to e.g. run some set number of threads (say 20) at a time and kill them if they lasted longer than a timeout period? If you killed a process being run from inside a thread, would the thread terminate? Or does the thread->kill() function successfully terminate the process and the thread simultaneously?