Created
August 18, 2024 12:32
-
-
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
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
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