TimeOut a Function, import signal?

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

The Join will NOT abort the thread if it is blocked on IO. So the line “it.join(timeout_duration)” should in theory never return, or if it does it might its own timeout meaning it gave up and the join failed.

The Connect X4 does not have rich enough thread support for you to try to routinely kill tasks unless you plan to reboot the X4.

You will need to manually enable the timeout with the TESTFUNC module.

thanks lynnl vor your answer.
what do you mean with “manually enable the timeout”?

At other Python Resource Pages, people are using the signal module (which is not available on this Digi Gateway) for doing a timeout. Its a bit confusing not to know which Python functions a can use on this platform [:(]