in reply to Win32: timing out a perl subroutine

Have you looked at Win32::Process? If I'm interpreting you correctly, this seems to give you what you want:
use Win32::Process; Win32::Process::Create($ProcessObj, "c:\\Perl\\bin\\perl.exe", "perl -e \"print q(yr code here\n)\"", 1, NORMAL_PRIORITY_CLASS, ".") $a = $ProcessObj->Wait(1000); # units = ms $ProcessObj->Kill($some_exit_code); print "[$a] ==> I *think* this will be zero if the " . " process terminated on its own before Wait() finished\n";
If the code in the $ProcessObj calls exit() on its own, the exit value will be retreivable via:
$ProcessObj->GetExitCode($exitvalue); print "The code returned: $exitvalue\n";
Not the most elegant solution (what with having to respecify the path for perl.exe, and I assume it's more memory intensive) but that's Win32 for you.

N.B.: That '1' means that the new process inherits the same filehandles as the code that produces it. If you don't want that, change it to '0'.

There's a lot of additional flags besides NORMAL_PRIORITY_CLASS. In particular, you can use

DETACHED_PROCESS CREATE_NEW_CONSOLE DETACHED_PROCESS | IDLE_PRIORITY_CLASS
(Note that's a bitwise or in the last example.) These flags are really poorly documented - I recommend finding one of Dave Roth's Win32 books).

-- Frag.

Replies are listed 'Best First'.
Re: Re: Win32: timing out a perl subroutine
by osfameron (Hermit) on Jun 20, 2001 at 14:42 UTC
    Thanks: I agree that the Win32::Process module is really badly documented - Dave Roth's documentations is much better.

    <Aside>and that's an issue for many of the Win32 modules... For example, Win32::Console is a good module, but it makes me tear my hair out trying to get it to do what I want: if I ever have time I'd re-document it.</Aside>

    But though that's a useful code snippet which I'll file away for future use, it doesn't allow me to return a value from a subroutine within the current perl script. Unix users, using the alarm() implementation of timeouts, have no problem doing a pre-emptive timeout of any arbitrary code.

    I'd be interested to hear from other my $os!~/l?[ui]n[ui]x/ users about how/if they can do timeouts on their perl.

    Cheerio!
    Osfameron

      I've posted a module for review which is a very scrappy and buggy first try at implementing this sort of timeout. Comments/review welcome!

      Update Well no comments as yet, but I've found even more bugs... hey ho

      Cheerio!
      Osfameron