python-cookbook
python-cookbook copied to clipboard
what the function of "flag" in example2 how_to_determine_if_a_thread_has_started?
I don't understand the function of flag in example2 how_to_determine_if_a_thread_has_started (12.2). The scripts works fine when I remove the flag part likes below.
import threading
import time
class PeriodicTimer:
def __init__(self, interval):
self._interval = interval
self._cv = threading.Condition()
def start(self):
t = threading.Thread(target=self.run)
t.daemon = True
t.start()
def run(self):
print('running start')
while True:
time.sleep(self._interval)
with self._cv:
self._cv.notify_all()
def wait_fot_tick(self):
with self._cv:
self._cv.wait()
ptimer = PeriodicTimer(5)
ptimer.start()
def countdown(nticks):
print('countdown start')
while nticks > 0:
ptimer.wait_fot_tick()
print('T-minus', nticks)
nticks -= 1
def countup(last):
n = 0
print('counting start')
while n < last:
ptimer.wait_fot_tick()
print('Counting', n)
n += 1
threading.Thread(target=countdown, args=(10,)).start()
threading.Thread(target=countup, args=(5,)).start()