From 4584c0ef55fa3a9d911aa945ce6b342057f03c04 Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Sun, 27 Mar 2022 16:02:29 -0400 Subject: [PATCH 1/8] fix start --- mo_threads/threads.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mo_threads/threads.py b/mo_threads/threads.py index b306556..355e850 100644 --- a/mo_threads/threads.py +++ b/mo_threads/threads.py @@ -236,7 +236,6 @@ def __init__(self, name, target, *args, **kwargs): else: self.parent = Thread.current() self.parent.add_child(self) - self.please_stop.then(self.start) def __enter__(self): return self @@ -282,7 +281,6 @@ def _run(self): if hook: sys.settrace(hook) - self.please_stop.remove_then(self.start) self.id = get_ident() with RegisterThread(self): try: From ba02b7d62d92efd44954c7f739f340583a49fe40 Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Sun, 27 Mar 2022 16:24:41 -0400 Subject: [PATCH 2/8] try this --- tests/test_threads.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_threads.py b/tests/test_threads.py index 20c29a5..8d23ea7 100644 --- a/tests/test_threads.py +++ b/tests/test_threads.py @@ -197,12 +197,14 @@ def test_start_stopped_thread(self): list_log = StructuredLogger_usingList() old_log, Log.main_log = Log.main_log, list_log old_log.stop() + print("ready") def worker(please_stop): Log.info("started") please_stop = Signal() please_stop.go() + print("runing") thread = Thread.run("work", worker, please_stop=please_stop) thread.stopped.wait() self.assertIn("started", Log.main_log.lines) From 84ed9ed6c279e5a9e4b31bf7be34e5a390ff8b8a Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Sun, 27 Mar 2022 16:43:27 -0400 Subject: [PATCH 3/8] try this --- tests/__init__.py | 2 +- tests/test_threads.py | 21 +++++---------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index cf4fb90..a12e8c4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -23,4 +23,4 @@ def write(self, template, params): self.lines.append(expand_template(template, params)) def stop(self): - pass + self.lines.append("logger stopped") diff --git a/tests/test_threads.py b/tests/test_threads.py index 8d23ea7..55aa5c4 100644 --- a/tests/test_threads.py +++ b/tests/test_threads.py @@ -13,10 +13,7 @@ from __future__ import unicode_literals from mo_future import text -from mo_json import value2json from mo_logs import Log -from mo_logs.log_usingNothing import StructuredLogger -from mo_logs.strings import expand_template from mo_testing.fuzzytestcase import FuzzyTestCase from mo_times.dates import Date from mo_times.durations import SECOND @@ -191,20 +188,23 @@ def test_start_stopped_thread(self): We often spawn threads to do work; ensure the thread is at least started, let the thread decide how to balance please_stop and the work to be done """ + print("stopping", flush=True) stop_main_thread() threads.MAIN_THREAD.stopped.wait() + print("starting", flush=True) start_main_thread() + print("logging", flush=True) list_log = StructuredLogger_usingList() old_log, Log.main_log = Log.main_log, list_log old_log.stop() - print("ready") + print("ready", flush=True) def worker(please_stop): Log.info("started") please_stop = Signal() please_stop.go() - print("runing") + print("running", flush=True) thread = Thread.run("work", worker, please_stop=please_stop) thread.stopped.wait() self.assertIn("started", Log.main_log.lines) @@ -232,14 +232,3 @@ def test_failure_during_wait_for_shutdown(self): def bad_worker(please_stop): raise Exception("bad worker failure") - - -class StructuredLogger_usingList(StructuredLogger): - def __init__(self): - self.lines = [] - - def write(self, template, params): - self.lines.append(expand_template(template, params)) - - def stop(self): - self.lines.append("logger stopped") From f7f3219369ee088f38e7462d0b4bba32ac6f7cc1 Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Sun, 27 Mar 2022 16:54:14 -0400 Subject: [PATCH 4/8] try this --- tests/test_threads.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/tests/test_threads.py b/tests/test_threads.py index 55aa5c4..dac4142 100644 --- a/tests/test_threads.py +++ b/tests/test_threads.py @@ -188,26 +188,16 @@ def test_start_stopped_thread(self): We often spawn threads to do work; ensure the thread is at least started, let the thread decide how to balance please_stop and the work to be done """ - print("stopping", flush=True) - stop_main_thread() - threads.MAIN_THREAD.stopped.wait() - print("starting", flush=True) - start_main_thread() - print("logging", flush=True) - list_log = StructuredLogger_usingList() - old_log, Log.main_log = Log.main_log, list_log - old_log.stop() - print("ready", flush=True) + done = [] def worker(please_stop): - Log.info("started") + done.append("started") please_stop = Signal() please_stop.go() - print("running", flush=True) thread = Thread.run("work", worker, please_stop=please_stop) thread.stopped.wait() - self.assertIn("started", Log.main_log.lines) + self.assertIn("started", done) def test_failure_during_wait_for_shutdown(self): threads.DEBUG = True From 17ba42d3bf3d3fb89dea52b9c0d835559e46304f Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Sun, 27 Mar 2022 17:10:35 -0400 Subject: [PATCH 5/8] try this --- mo_threads/threads.py | 3 ++- tests/test_threads.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mo_threads/threads.py b/mo_threads/threads.py index 355e850..89c547c 100644 --- a/mo_threads/threads.py +++ b/mo_threads/threads.py @@ -620,7 +620,8 @@ def start_main_thread(): with ALL_LOCK: if ALL: - raise Exception("failure") + names = [t.name for k, t in ALL.items()] + raise Exception(f"expecting no threads {names}") ALL[get_ident()] = MAIN_THREAD diff --git a/tests/test_threads.py b/tests/test_threads.py index dac4142..bbb9c22 100644 --- a/tests/test_threads.py +++ b/tests/test_threads.py @@ -201,7 +201,8 @@ def worker(please_stop): def test_failure_during_wait_for_shutdown(self): threads.DEBUG = True - threads.MAIN_THREAD.stop() + stop_main_thread() + start_main_thread() list_log = StructuredLogger_usingList() old_log, Log.main_log = Log.main_log, list_log From 2a3bae6089753bdc463deac09ff37294eabc19fc Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Mon, 28 Mar 2022 19:19:05 -0400 Subject: [PATCH 6/8] try this --- mo_threads/threads.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/mo_threads/threads.py b/mo_threads/threads.py index 89c547c..1919d97 100644 --- a/mo_threads/threads.py +++ b/mo_threads/threads.py @@ -179,16 +179,19 @@ def stop(self): ) with self.shutdown_locker: - if self.stopped: - return - self.stop_logging() - self.timers.stop().join() - - if cprofiler_stats is not None: - from mo_threads.profiles import write_profiles - write_profiles(self.cprofiler) - DEBUG and Log.note("Thread {{name|quote}} now stopped", name=self.name) - self.stopped.go() + try: + if self.stopped: + return + self.stop_logging() + self.timers.stop().join() + + if cprofiler_stats is not None: + from mo_threads.profiles import write_profiles + write_profiles(self.cprofiler) + DEBUG and Log.note("Thread {{name|quote}} now stopped", name=self.name) + self.stopped.go() + except Exception as cause: + join_errors.append(cause) with ALL_LOCK: del ALL[self.id] From 8d07c9a2d1e09455f5ff6966da608fb0c2e90bb5 Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Mon, 28 Mar 2022 19:31:21 -0400 Subject: [PATCH 7/8] try this --- tests/test_threads.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_threads.py b/tests/test_threads.py index bbb9c22..dd46148 100644 --- a/tests/test_threads.py +++ b/tests/test_threads.py @@ -218,8 +218,6 @@ def test_failure_during_wait_for_shutdown(self): self.assertIn("ERROR", list_log.lines[-2]) self.assertEqual(bool(threads.MAIN_THREAD.timers.stopped), True) - start_main_thread() - def bad_worker(please_stop): raise Exception("bad worker failure") From 327170b0f66f8d5ac39ed9e4fd3f87a062a4e5ed Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Mon, 28 Mar 2022 19:55:53 -0400 Subject: [PATCH 8/8] update version number --- setup.py | 2 +- setuptools.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 55b361e..90f0184 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,6 @@ name='mo-threads', packages=["mo_threads"], url='https://github.com/klahnakoski/mo-threads', - version='5.154.22086', + version='5.155.22087', zip_safe=False ) \ No newline at end of file diff --git a/setuptools.json b/setuptools.json index 810c803..bcc5b65 100644 --- a/setuptools.json +++ b/setuptools.json @@ -260,6 +260,6 @@ "name": "mo-threads", "packages": ["mo_threads"], "url": "https://github.com/klahnakoski/mo-threads", - "version": "5.154.22086", + "version": "5.155.22087", "zip_safe": false } \ No newline at end of file