okay. that’s looks a heck of a lot more elegant than i was anticipating. thank you very much. from what i gleaned, i’ve been tinkering:
import time
import threading
import random
inputSignal = False
def worker_function():
print(3*f'-----------> working ')
def worker_thread(delay, event):
#worker.wait() #<-- this is only good until it enters the loop... duh...
while True:
worker.wait() #<-- if this is NOT set, it bails right here
time.sleep(delay)
worker_function()
def manage_function(event):
print(f'\n|| inside management ||')
global inputSignal
if inputSignal: #<--toggles the worker thread
worker.set()
print(f'=== worker enabled ===')
else:
worker.clear()
print(f'=== worker disabled ===')
def management_thread(delay, event):
while True:
time.sleep(delay)
manage_function(event)
def signalClock(interval):
while True:
time.sleep(interval)
getInputSignal()
def getInputSignal():
global inputSignal
inputSignal = True if random.randint(0, 1) else False
print(f'\n►► inputSignal: {inputSignal} ◄◄\n')
return inputSignal
worker = threading.Event()
threading.Thread(target=worker_thread, args=(0.25, worker)).start()
threading.Thread(target=management_thread, args=(1.0, worker)).start()
threading.Thread(target=signalClock, args=(0.5,)).start()
while True: #<-- main application loop
time.sleep(0.1)
print(f'main loop')
once the worker thread starts, when the worker.clear()
is called, it doesn’t go back to waiting. what am i doing wrong? according to the docs, this should work:
clear()
Reset the internal flag to false. Subsequently, threads callingwait()
will block untilset()
is called to set the internal flag to true again.
EDIT: turns out, i put the worker.wait()
outside the loop instead of instead of inside the loop. i corrected the code to reflect this and commented the wrong line for contrast. and, of course, because of the different timings, it will take a few loops to pick up the change.
massive thanks to you guys for your helpful nudges. got me thinking, and thinking in the right direction.