h8a2n5k23
h8a2n5k23

Reputation: 77

Timeout function if it takes too long

Forgive me, I am a newbie. I've surveyed some solution. But it is so hard for me to understand and to modify that. (Or maybe there is no solution in line with my imagination?). And I hope it can work on Ubuntu & Win7.

There is an example like this.

import random,time

def example():
    while random.randint(0,10) != 1:
        time.sleep(1)
    print "down"

example()

And my imagination is...

If, the example() run over 10s, then rerun the example() again. (And maybe there is a place I can code anything else. Like I want to record the timeout event on TXT, and I can code the code at that place.) Else, do nothing.

Is it possible to do that?

Upvotes: 1

Views: 1526

Answers (2)

user4815162342
user4815162342

Reputation: 154856

You can run a watch-dog in a separate thread that interrupts the main thread (that runs example) when it exceeds the time limit. Here is a possible implementation, with timeout lowered to 3s for ease of debugging:

import time, threading, thread

def watchdog_timer(state):
    time.sleep(3)
    if not state['completed']:
        thread.interrupt_main()

def run_example():
    while True:
        state = {'completed': False}
        watchdog = threading.Thread(target=watchdog_timer, args=(state,))
        watchdog.daemon = True
        watchdog.start()
        try:
            example()
            state['completed'] = True
        except KeyboardInterrupt:
            # this would be the place to log the timeout event
            pass
        else:
            break

Upvotes: 3

Byte Commander
Byte Commander

Reputation: 6736

I'm not sure if I fully understood what you want to achieve, but as you're constantly looping and only have one short and predictable blocking command, you could simply store the time when the loop started and then compare it to the current time once per loop iteration. If the difference exceeds your limit, do whatever you want:

import random,time
time_limit=10

def example():
    time_start = time.time()  # store current time (seconds since 1970)
    while random.randint(0,10) != 1:
        time.sleep(1)
        if (time.time() >= time_start + time_limit):  # compare with current time
            print "canceled!"
            break  # break the while-loop
    print "down"

example()

Upvotes: 0

Related Questions