Timeout function if it takes too long

h8a2n5k23

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?

user4815162342

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

timeout if method takes too long to finish

From Dev

prop() takes too long

From Dev

removeFromSuperview() takes too long

From Dev

Wait until a BlockingCollection queue is cleared by a background thread, with a timeout if it takes too long?

From Dev

Wait until a BlockingCollection queue is cleared by a background thread, with a timeout if it takes too long?

From Dev

How do i make it stop and say connection timeout if it takes too long to connect?

From Dev

cURL takes too long to load

From Dev

XPages typeahead takes too long

From Dev

mpirun takes too long to run

From Dev

JQuery : $.get() takes too long

From Dev

Mouse takes too long to be discovered

From Dev

Why dd takes too long?

From Dev

AVCaptureSession commitConfiguration() takes too long

From Dev

mpirun takes too long to run

From Dev

RMySQL dbReadTable takes too long

From Dev

jarvis takes too long to respond

From Dev

My short recursive function takes too long to execute, how can I optimize it

From Dev

How do we improve a MongoDB MapReduce function that takes too long to retrieve data and gives out of memory errors?

From Dev

Cancel Parse Request If It Takes Too Long

From Dev

Selenium - parsing a page takes too long

From Java

Android Studio gradle takes too long to build

From Dev

Query by coordinates takes too long - options to optimize?

From Dev

AJAX request takes too long to complete the request

From Dev

Removing CONSTRAINT in SQL takes too long

From Dev

UITableView rowHeight animation takes too long

From Dev

Powershell, Stop-Job takes too long

From Dev

Php takes too long to be executed, need thread

From Dev

Core data save takes too long

From Dev

gulp browserify bundle time takes too long

Related Related

HotTag

Archive