Skip to content

Instantly share code, notes, and snippets.

@0D1NTR33
Created July 5, 2018 11:17
Show Gist options
  • Save 0D1NTR33/f457ef1994c6e62c6a3eef77c6f87f71 to your computer and use it in GitHub Desktop.
Save 0D1NTR33/f457ef1994c6e62c6a3eef77c6f87f71 to your computer and use it in GitHub Desktop.
setTimeout in Python
# 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