From 0b34aa7385b43c0ba155ebe493d5310fc5e32190 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 29 Jul 2020 20:36:41 -0500 Subject: [PATCH] added onionrthreads to replace communicator timers --- src/onionrthreads/__init__.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/onionrthreads/__init__.py diff --git a/src/onionrthreads/__init__.py b/src/onionrthreads/__init__.py new file mode 100644 index 00000000..aaef3956 --- /dev/null +++ b/src/onionrthreads/__init__.py @@ -0,0 +1,25 @@ +from typing import Callable +from typing import Iterable + +from threading import Thread + +from utils.bettersleep import better_sleep + + +def _onionr_thread(func: Callable, args: Iterable, + sleep: int, initial_sleep): + better_sleep(initial_sleep) + while True: + func(*args) + better_sleep(sleep) + + +def add_onionr_thread( + func: Callable, args: Iterable, + sleep: int, initial_sleep: int = 5): + """Spawn a new onionr thread that exits when the main thread does. + + Runs in an infinite loop with sleep between calls + Passes in an interable args and sleep variables""" + Thread(target=_onionr_thread, + args=(func, args, sleep, initial_sleep), daemon=True).start()