Created
July 5, 2018 11:17
-
-
Save 0D1NTR33/f457ef1994c6e62c6a3eef77c6f87f71 to your computer and use it in GitHub Desktop.
setTimeout in Python
This file contains hidden or 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
# http://fredericiana.com/2014/11/14/settimeout-python-delay/ | |
# utils.py | |
import threading | |
from functools import wraps | |
def delay(delay=0.): | |
""" | |
Decorator delaying the execution of a function for a while. | |
""" | |
def wrap(f): | |
@wraps(f) | |
def delayed(*args, **kwargs): | |
timer = threading.Timer(delay, f, args=args, kwargs=kwargs) | |
timer.start() | |
return delayed | |
return wrap | |
#main.py | |
from utils import delay | |
@delay(3.0) | |
def my_func(arg1, arg2): | |
print arg1, arg2 | |
if __name__ == '__main__': | |
my_func('Hello', 'world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment