summaryrefslogtreecommitdiff
path: root/examples/async_printing.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/async_printing.py')
-rwxr-xr-xexamples/async_printing.py14
1 files changed, 7 insertions, 7 deletions
diff --git a/examples/async_printing.py b/examples/async_printing.py
index 74c4f41f..bcdbc8f0 100755
--- a/examples/async_printing.py
+++ b/examples/async_printing.py
@@ -41,7 +41,7 @@ class AlerterApp(cmd2.Cmd):
self.prompt = "(APR)> "
# The thread that will asynchronously alert the user of events
- self._stop_thread = False
+ self._stop_event = threading.Event()
self._alerter_thread = threading.Thread()
self._alert_count = 0
self._next_alert_time = 0
@@ -56,7 +56,7 @@ class AlerterApp(cmd2.Cmd):
# Therefore this is the best place to start the alerter thread since there is no risk of it alerting
# before the prompt is displayed. You can also start it via a command if its not something that should
# be running during the entire application. See do_start_alerts().
- self._stop_thread = False
+ self._stop_event.clear()
self._alerter_thread = threading.Thread(name='alerter', target=self._alerter_thread_func)
self._alerter_thread.start()
@@ -67,7 +67,7 @@ class AlerterApp(cmd2.Cmd):
# After this function returns, cmdloop() releases self.terminal_lock which could make the alerter
# thread think the prompt is on screen. Therefore this is the best place to stop the alerter thread.
# You can also stop it via a command. See do_stop_alerts().
- self._stop_thread = True
+ self._stop_event.set()
if self._alerter_thread.is_alive():
self._alerter_thread.join()
@@ -76,13 +76,13 @@ class AlerterApp(cmd2.Cmd):
if self._alerter_thread.is_alive():
print("The alert thread is already started")
else:
- self._stop_thread = False
+ self._stop_event.clear()
self._alerter_thread = threading.Thread(name='alerter', target=self._alerter_thread_func)
self._alerter_thread.start()
def do_stop_alerts(self, _):
""" Stops the alerter thread """
- self._stop_thread = True
+ self._stop_event.set()
if self._alerter_thread.is_alive():
self._alerter_thread.join()
else:
@@ -172,7 +172,7 @@ class AlerterApp(cmd2.Cmd):
self._alert_count = 0
self._next_alert_time = 0
- while not self._stop_thread:
+ while not self._stop_event.is_set():
# Always acquire terminal_lock before printing alerts or updating the prompt
# To keep the app responsive, do not block on this call
if self.terminal_lock.acquire(blocking=False):
@@ -197,7 +197,7 @@ class AlerterApp(cmd2.Cmd):
# Don't forget to release the lock
self.terminal_lock.release()
- time.sleep(0.5)
+ self._stop_event.wait(0.5)
if __name__ == '__main__':