Adjusted onionrthreads to take kwargs

This commit is contained in:
Kevin F 2022-02-09 19:28:31 -06:00
parent 4fba79950c
commit e55beec18c
2 changed files with 15 additions and 9 deletions

View File

@ -10,14 +10,14 @@ from time import sleep
import logger
def _onionr_thread(func: Callable, args: Iterable,
sleep_secs: int, initial_sleep):
def _onionr_thread(func: Callable,
sleep_secs: int, initial_sleep, *args, **kwargs):
thread_id = str(uuid4())
if initial_sleep:
sleep(initial_sleep)
while True:
try:
func(*args)
func(*args, **kwargs)
except Exception as _: # noqa
logger.warn(
f"Onionr thread exception in {thread_id} \n" +
@ -27,11 +27,18 @@ def _onionr_thread(func: Callable, args: Iterable,
def add_onionr_thread(
func: Callable, args: Iterable,
sleep_secs: int, initial_sleep: int = 5):
func: Callable,
sleep_secs: int, *args, initial_sleep: int = 5, **kwargs):
"""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_secs, initial_sleep), daemon=True).start()
args=(
func,
sleep_secs,
initial_sleep,
*args),
kwargs=kwargs,
daemon=True).start()

View File

@ -25,7 +25,7 @@ class OnionrThreadsTests(unittest.TestCase):
def _test_func(obj_list):
obj_list.append(1)
add_onionr_thread(_test_func, (l,), 0.05, 0)
add_onionr_thread(_test_func, 0.05, l, initial_sleep=0)
sleep(0.05)
self.assertGreaterEqual(len(l), 1)
@ -34,8 +34,7 @@ class OnionrThreadsTests(unittest.TestCase):
def _test_func(obj_list):
obj_list.append(1)
add_onionr_thread(_test_func, (l,), 0.05, 0.1)
sleep(0.06)
add_onionr_thread(_test_func, 0.05, l, initial_sleep=1)
self.assertEqual(len(l), 0)