Similar topics have already been discussed relating to timing out on Perlmonks processes (e.g. the Net::Ping discussion) and user input (with Term::ReadKey etc.).

The problem is that alarm() is not implemented on Win32 Perl. Possible workarounds are forking off a subprocess (from Perl 5.6 possibled in Win32) and killing it if its still alive after timeout.

But I've not come across any discussion (and apologies if I've just missed it) about timing out a Perl sub. That is, I want to get back the result of an arbitrary subrouting call - so I can't fork off a disassociated process.

# example snippet. Doesn't refer to a real function yet... my $result = timeout ( time =>1.5, sub =>sub { # do something complicated here # that may take a long time return $whatever; }, timed_out => "Function timed out" );
e.g. $result should either return the result of the sub in the code reference, or the value of the 'timed_out' parameter (most likely undef).

What I've tried

I tried using threads, because I'd understood that this was how Win32 'fork' emulation was done... but I get a "No threads in this Perl" error. (perl -v gives me "v5.6.0 built for MSWin32-x86-multi-thread ... Binary build 616 provided by ActiveState")

I thought of the following:
- fork child
- parent runs sub
- parent then kills child
- child sleeps till timeout
- child wakes up (unless it's been killed)
- child kills parent. (e.g. the child then takes over as a new parent process, as with daemonization)

But I don't think a child can kill the parent? Alternatively, the child could flag to the parent that it will now take over thank you very much, so that if/when the parent returns it will just exit. But I'm confused on the details of IPC. I've read perlipc, and it's very inspiring, but as 60-70% of it is Unix-only, it's a little confusing for us Win32 types.
(Anyone know of any good Win32 Perl IPC tutorials out there?)

So does anyone have any suggestions, even pointers of where to rtfm?

Cheerio!
Osfameron


In reply to Win32: timing out a perl subroutine by osfameron

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.