Hey Digi Developpers,
I’m developping a data-logger application of several sensors running on a ConnectPort X4 Gateway in python.
In this context i need to timeout a function. I’m using this Code:
import sys
import time
import threading
def testfunc():
i = 0
while i<50:
print "test"
time.sleep(0.2)
i = i +1
def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
def run(self):
try:
self.result = func(*args, **kwargs)
except:
self.result = default
it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return default
else:
return it.result
########################### MAIN ####################################
print "started"
x = timeout(testfunc, timeout_duration=5) #execute testfunc with a timeout of 5 seconds
print "end"
sys.exit()
I want to timeout the function “testfunc”. What it does is:
- prints “started”
- excecutes testfunc for 5 seconds printing “test” on the console.
- After these 5 seconds the programm just hangs
- “end” is never printed
I don’t know where I made a mistake. I hope someone can help me…
A different kind of timeout which works would be a solution for me too.
I read some solutions on other python development pages using the “signal” module but I couldn’t find the signal modul for digi developping (its not built-in?!), so these solutions didn’t work… :-/
Need Help!
Python v2.4.3