By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

module signal

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
> 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."
Jul 18 '05 #4

P: n/a
> 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
Jul 18 '05 #5

P: n/a
> 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.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.