module signal
Peter Otten
__peter__ at web.de
Tue Oct 26 05:19:06 EDT 2004
Klaus Neuner wrote:
> # (2)
>
> import sys, signal
>
> def alarm_handler(signum, frame):
> raise
>
> try:
> signal.signal(signal.SIGALRM, alarm_handler)
> signal.alarm(3)
> re.search("a(((.)*c)*d)*e", "abcdf"*20)
> except:
> print "Time over."
>
> #############################################################
>
> (1) behaves the way one would expect it to behave: It stops counting
> after about 3 seconds. (2) will stop trying to match the regexp at
> once. And this behaviour will not change if you give any other
> argument to signal.alarm().
>
> Why?
To remind you that a bare except is always a bad idea. Modify your program
along these lines
class Timeout(Exception): pass
def alarm_handler(signum, frame):
raise Timeout
try
...
except Timeout:
print "Time over."
to see that you didn't import the re module which raises a NameError.
Unfortunately the resulting script does not terminate anymore and I cannot
help you with that.
Peter
More information about the Python-list
mailing list