diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-02-19 21:35:13 -0500 |
|---|---|---|
| committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2021-02-19 21:35:13 -0500 |
| commit | 3e180a810e9c4b9d251c135667d1d150b0bbd0dd (patch) | |
| tree | 03e49d5da86d40efa9118eccfd8bd4bbf3dcf86b /examples/async_printing.py | |
| parent | 4c70bdb03d34c43f833bf77c441452cd402d0715 (diff) | |
| parent | 06aaf962689840631325c70ea7e9056d176c7f67 (diff) | |
| download | cmd2-git-3e180a810e9c4b9d251c135667d1d150b0bbd0dd.tar.gz | |
Merge branch 'master' into black
# Conflicts:
# cmd2/__init__.py
# cmd2/argparse_completer.py
# cmd2/argparse_custom.py
# cmd2/cmd2.py
# cmd2/decorators.py
# cmd2/exceptions.py
# cmd2/utils.py
# examples/arg_decorators.py
# examples/argparse_completion.py
# examples/modular_commands_main.py
# tests/test_argparse_completer.py
# tests/test_argparse_custom.py
# tests/test_cmd2.py
# tests/test_completion.py
# tests/test_history.py
Diffstat (limited to 'examples/async_printing.py')
| -rwxr-xr-x | examples/async_printing.py | 14 |
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__': |
