osfameron has asked for the wisdom of the Perl Monks concerning the following question:
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.
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).# 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" );
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32: timing out a perl subroutine
by frag (Hermit) on Jun 19, 2001 at 20:11 UTC | |
by osfameron (Hermit) on Jun 20, 2001 at 14:42 UTC | |
by osfameron (Hermit) on Jun 21, 2001 at 13:23 UTC | |
|
Re: Win32: timing out a perl subroutine
by Brovnik (Hermit) on Jun 19, 2001 at 19:18 UTC | |
|
Re: Win32: timing out a perl subroutine
by Eradicatore (Monk) on Jun 19, 2001 at 18:08 UTC |