Skip to content

Instantly share code, notes, and snippets.

@dnmellen
Created January 23, 2014 15:39
Show Gist options
  • Save dnmellen/8580720 to your computer and use it in GitHub Desktop.
Save dnmellen/8580720 to your computer and use it in GitHub Desktop.
Threaded timeout Python decorator
import threading
import logging
from functools import wraps
logger = logging.getLogger(__name__)
def timeout(secs=None):
def my_decorator(target, *args, **kwargs):
def wrapper(*args, **kwargs):
t = threading.Thread(target=target, args=args, kwargs=kwargs)
t.start()
returned_value = t.join(secs)
if t.is_alive():
logger.warning('Timeout in thread running {}'.format(target))
return returned_value
return wraps(target)(wrapper)
return my_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment