Click here to get back home

eval EXPR with maximum execution time?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
eval EXPR with maximum execution time? alx__21 02-06-2008
Posted by alx__21 on February 6, 2008, 3:40 pm
Please log in for more thread options
Hi,

I'm looking for something that is like eval in that it can compile and
run code from strings, but will quit after a maximum given time.

The benchmark module tool "timethis" is very close, but it can only
ever specify a minimum time to iterate the given code. What I want is
code that iterates ONCE, breaking at a maximum time that I can specify
(if it runs that long).

If anybody knows of a function to do this I would appreciate it. I
would rather not have to start digging around in the source for
timethis to create my own.

Thanks.

Posted by Joost Diepenmaat on February 6, 2008, 3:47 pm
Please log in for more thread options
alx__21@hotmail.com writes:

> Hi,
>
> I'm looking for something that is like eval in that it can compile and
> run code from strings, but will quit after a maximum given time.
>
> The benchmark module tool "timethis" is very close, but it can only
> ever specify a minimum time to iterate the given code. What I want is
> code that iterates ONCE, breaking at a maximum time that I can specify
> (if it runs that long).
>
> If anybody knows of a function to do this I would appreciate it. I
> would rather not have to start digging around in the source for
> timethis to create my own.
>
> Thanks.

See perldoc -f alarm, but note that your code may still block on IO, as
far as I know, if your perl was build with "safe signals" - the default
for a few years now.

Joost.


Posted by Joost Diepenmaat on February 6, 2008, 3:50 pm
Please log in for more thread options
> See perldoc -f alarm, but note that your code may still block on IO, as
> far as I know, if your perl was build with "safe signals" - the default
> for a few years now.
>
> Joost.

For a possibly more robust (or at least, more certain way of stopping
processing) you could fork() the offending process and kill() it from
the parent if it doesn't stop after a sleep().

If you want to get any useful data back from the fork()ed child process,
you need some kind of IPC. See the perlopen and perlipc man pages.

Joost.

Posted by Ben Morrow on February 6, 2008, 4:48 pm
Please log in for more thread options

> > See perldoc -f alarm, but note that your code may still block on IO, as
> > far as I know, if your perl was build with "safe signals" - the default
> > for a few years now.

Using POSIX::SigAction it is possible to install an unsafe handler for
SIGALARM, which will allow you to break out of IO. Of course, all the
traditional caveats about unsafe signals then apply: it would be best to
do nothing more in the sighandler than die to break out of the eval.

> For a possibly more robust (or at least, more certain way of stopping
> processing) you could fork() the offending process and kill() it from
> the parent if it doesn't stop after a sleep().
>
> If you want to get any useful data back from the fork()ed child process,
> you need some kind of IPC. See the perlopen and perlipc man pages.

If you make the child the watchdog instead, there's no need for this. It
also makes it easier to clean up after a successful completion. Of
course, it makes it slightly *harder* to clean up after an unsuccessful
completion: you're pretty much back to signalling the parent, catching
that signal unsafely, and breaking out of the eval. See above :). If
you're not using alarm for anything else (on many systems, sleep uses
alarm internally) it is probably the best solution.

Ben


Posted by alx__21 on February 6, 2008, 4:54 pm
Please log in for more thread options
Thank you!
As I'm not doing any IO inside the eval (just a simple return at the
end), I think that alarm will work well.

Similar ThreadsPosted
how to limit system() execution time? May 22, 2005, 10:50 pm
GMT time to local time, according to timezone and summer/winter time. May 15, 2005, 10:45 pm
my VAR = EXPR if COND July 22, 2006, 1:39 pm
reg expr, extract digit from string. March 18, 2005, 8:18 am
reg expr, extract word from string October 7, 2005, 4:12 am
value or reference inside foreach ? December 15, 2007, 1:24 am
Different behavior between eval "07" and eval "08" February 1, 2008, 9:59 pm
Prototyping Subs as func expr, list As In map? August 3, 2006, 3:15 pm
Calculating time of employee session from the log date/time stampusing perl May 24, 2005, 5:55 pm
How do I convert TIME into Cookie and last-modified-time format? October 1, 2004, 3:05 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap