K
Klaus Neuner
Hello,
consider the following two programs:
# (1)
import sys, signal
def alarm_handler(signum, frame):
raise
try:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(3)
n = 0
while 1:
print n
n = n+1
except:
print "Time over."
#############################################################
# (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?
Another program of mine contains code that is fairly analogous to (2)
(or at least I thought so). Here it is:
# (3)
def main():
# read in a corpus file
# compile some regexp patterns
# and then, for each pattern do:
for line in corpus:
try:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(10)
test = pattern.search(line) #!
if not test:
out.append(line)
else:
out.append(some_function(line, pattern))
except:
out.append(line)
#############################################################
The regexps pattern in code (3) use to be rather complicated. When
code (3) gets really long lines as input, Python will be executing
line #! for days. Why?
What I am looking for is code with the following meaning:
for n seconds try to do:
# any Python code may be inserted here
print "bla"
When the n seconds are over, Python should print "bla". It should not
exit the program. How can this be done?
Klaus
consider the following two programs:
# (1)
import sys, signal
def alarm_handler(signum, frame):
raise
try:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(3)
n = 0
while 1:
print n
n = n+1
except:
print "Time over."
#############################################################
# (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?
Another program of mine contains code that is fairly analogous to (2)
(or at least I thought so). Here it is:
# (3)
def main():
# read in a corpus file
# compile some regexp patterns
# and then, for each pattern do:
for line in corpus:
try:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(10)
test = pattern.search(line) #!
if not test:
out.append(line)
else:
out.append(some_function(line, pattern))
except:
out.append(line)
#############################################################
The regexps pattern in code (3) use to be rather complicated. When
code (3) gets really long lines as input, Python will be executing
line #! for days. Why?
What I am looking for is code with the following meaning:
for n seconds try to do:
# any Python code may be inserted here
print "bla"
When the n seconds are over, Python should print "bla". It should not
exit the program. How can this be done?
Klaus