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

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?

Replies are listed 'Best First'.
Re^3: Win32::Process output
by BrowserUk (Patriarch) on Nov 03, 2010 at 20:02 UTC

    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?

        Let me see if I understand you correctly.

        • You have a whole bunch of commands (more than 20) that you want to run concurrently, but never more than 20 at a time.
        • You want to gather the output from those commands.
        • If they do not complete within some specified time, you want to terminate them.
        • If they do not complete, you aren't interested in any output they may have produced before being killed.
        • The program is already using thread for other things.

        A few questions:

        • Do the other threads in the program count towards your limit of 20? Or does that limit only apply to concurrent comamnds?
        • Are these commands the same executable with different parameters? If so, my I ask what executable?

        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.