Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

(bbfu) (nested limits) Re: Perl syntax for enforcing time limits

by bbfu (Curate)
on Apr 30, 2001 at 07:34 UTC ( [id://76540]=note: print w/replies, xml ) Need Help??


in reply to Perl syntax for enforcing time limits

An interesting module, and implemented well. As constructive critisim, there's two things I'd like to mention.

First, and simplest, you document a return value of undef on timeout but you actually return 0. This can trip people up so you might want to fix it.

Second, I think that, perhaps, you're handling nested time_limit()'s incorrectly (not that I really know what I mean by "correct"). Consider:

#!/usr/bin/perl -w use TimeLimit; $outter1 = time; time_limit { $inner1 = time; time_limit { sleep 7 } 5; $inner2 = time; } 3; $outter2 = time; $elapsed_outter = $outter2-$outter1; $elapsed_inner = $inner2-$inner1; print "Seconds elapsed for outter limit: $elapsed_outter\n"; print "Seconds elapsed for inner limit: $elapsed_inner\n";

The output of this test program is:

Seconds elapsed for outter limit: 5 Seconds elapsed for inner limit: 5

Whereas anyone looking at the outter time_limit() would think that it should return no more than 3s later. (Imagine the inner time_limit() call being burried in a called subroutine...)

Further, what if the code you expected to cause a timeout was after the inner time_limit() call? Because your code actually resets the alarm(), it could take even longer.

Of course, for most applications where you would want to time_limit() some code, you wouldn't really care if it takes a little bit longer to time out than you expected, so it shouldn't matter much. Also, I can't think of a very elegant solution that would handle such cases. I never did understand why they limited it to one alarm per process...

Well, I hope that at least gives you some ideas to work with. In fact, I think I might just have to look into writing a module that would offer a more robust replacement for alarm. :-) And again, congrats on a good module.

Happy coding.

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://76540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-03-28 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found