2020-07-30 01:36:41 +00:00
|
|
|
from typing import Callable
|
|
|
|
from typing import Iterable
|
|
|
|
|
|
|
|
from threading import Thread
|
|
|
|
|
2020-07-31 01:15:36 +00:00
|
|
|
from time import sleep
|
2020-07-30 01:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _onionr_thread(func: Callable, args: Iterable,
|
2020-07-31 01:15:36 +00:00
|
|
|
sleep_secs: int, initial_sleep):
|
|
|
|
if initial_sleep:
|
|
|
|
sleep(initial_sleep)
|
2020-07-30 01:36:41 +00:00
|
|
|
while True:
|
|
|
|
func(*args)
|
2020-07-31 01:15:36 +00:00
|
|
|
sleep(sleep_secs)
|
2020-07-30 01:36:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_onionr_thread(
|
|
|
|
func: Callable, args: Iterable,
|
2020-07-31 01:15:36 +00:00
|
|
|
sleep_secs: int, initial_sleep: int = 5):
|
2020-07-30 01:36:41 +00:00
|
|
|
"""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,
|
2020-07-31 01:15:36 +00:00
|
|
|
args=(func, args, sleep_secs, initial_sleep), daemon=True).start()
|