Created
January 23, 2014 15:39
-
-
Save dnmellen/8580720 to your computer and use it in GitHub Desktop.
Threaded timeout Python decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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