Skip to content

Instantly share code, notes, and snippets.

@nevikw39
Created August 18, 2024 12:32
Show Gist options
  • Save nevikw39/d8b8ab5b065dfd9c198d28053568053c to your computer and use it in GitHub Desktop.
Save nevikw39/d8b8ab5b065dfd9c198d28053568053c to your computer and use it in GitHub Desktop.
Python function decorator to execute a function in another thread at every given interval
from collections.abc import Callable
from threading import Timer, Event
def interval(sec: int, event: Event = None):
def decorator(func: Callable):
timer: Timer
def wrapper(*args, **kwargs):
if event and event.is_set(): return
func(*args, **kwargs)
nonlocal timer
timer = Timer(sec, wrapper)
timer.start()
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment