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

another try: "timout actions"

P: n/a
Hello,

as I am desperately searching for a (straightforward) solution of the
following problem, I'll give it another try:

How can I tell Python to do things of the following kind:

for n seconds try to do:
somefunc()
other_func()

somefunc() should be allowed to imply ANY activity Python is
capable to do. When the n seconds are over, Python should not exit the
program but execute other_func().

It turned out that the module signal is a solution for some but not
for all instances of somefunc(): If somefunc() contains a regexp match
that does not terminate (in a reasonable time), then the program will
not go on to other_func() (after n seconds). You can verify this by
running the programs (1) and (2) given below.

So, what other ways are there to code actions of the type described
above? Which way would be the cleanest and most straightforward? (I
don't want to use a program call, because this would decrease
readability, portability and other good properties a lot.)

Thanks in advance and sorry for asking the same question twice.

Klaus
################################################## ###########

# (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 #1
Share this Question
Share on Google+
1 Reply


P: n/a
So, what other ways are there to code actions of the type described
above? Which way would be the cleanest and most straightforward? (I
don't want to use a program call, because this would decrease
readability, portability and other good properties a lot.)


The only thing I can think of is to delegate the work to a therad, and wait
the specified period of time. If by then the result is computed, use it.
Otherwise continue - and ignore the thread. I admit that that is a ugly
solution - especially when the computation is time intensive.

Maybe some gurus can elaborate on why the regexp-search isn't interrupted by
the signal - the only thing I can think of is that the signal is deferred
until the interpreter re-enters its bytecode-loop - but why the search is
so atomic I don't know.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.