module signal

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
 
P

Peter Otten

Klaus said:
# (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
 
J

Josef Meile

Hi Klaus,
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?
It should work with a signal alarm; I don't know why it
doesn't. You could also try a Timer object from the
threading class; it's even easier than a signal alarm.

Regards,
Josef
 
K

Klaus Neuner

to see that you didn't import the re module which raises a NameError.

O.k. Sorry for that. Sometimes one doesn't see the most obvious things.

So I should reformulate my problem as follows:

Why does (2) not work (though (1) does)?

# (1)

import sys, signal


class Timeout(Exception):
pass

def alarm_handler(signum, frame):
raise Timeout

try:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(3)
n = 0
while 1:
print n
n = n+1
except Timeout:
print "Time over."

#############################################################

# (2)

import sys, signal, re

class Timeout(Exception):
pass

def alarm_handler(signum, frame):
raise Timeout

try:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(3)
re.search("a(((.)*c)*d)*e", "abcdf"*20)
except Timeout:
print "Time over."
 
K

Klaus Neuner

doesn't. You could also try a Timer object from the
threading class; it's even easier than a signal alarm.

I don't see how to do it with a Timer object only. Could you rewrite
the regexp example with a Timer object such that it works?

Klaus
 
J

Josef Meile

I don't see how to do it with a Timer object only. Could you rewrite
the regexp example with a Timer object such that it works?
Oh yes, I'm wrong. I confused this module with something else, sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top