diff --git a/portal/contextlib.py b/portal/contextlib.py index c427c1b..f5100db 100644 --- a/portal/contextlib.py +++ b/portal/contextlib.py @@ -4,6 +4,7 @@ import sys import threading import traceback +import warnings import cloudpickle import psutil @@ -143,7 +144,12 @@ def add_worker(self, worker): return if hasattr(worker, 'thread'): assert current != worker.thread - current.children.append(worker) + try: + current.children.append(worker) + except AttributeError: + warnings.warn( + 'Using Portal from plain Python threads is discouraged because ' + + 'they can cause hangs during shutdown.') def children(self, thread): current = thread or threading.current_thread() diff --git a/tests/test_process.py b/tests/test_process.py index 68db62a..7c93a0d 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -52,7 +52,7 @@ def fn(): worker = portal.Process(fn, start=True) worker.kill() assert not worker.running - assert worker.exitcode < 0 + assert abs(worker.exitcode) >= 1 @pytest.mark.parametrize('repeat', range(5)) def test_kill_with_subproc(self, repeat): @@ -80,7 +80,7 @@ def inner(ready, queue): ready.wait() worker.kill() assert not worker.running - assert worker.exitcode < 0 + assert abs(worker.exitcode) >= 1 assert not portal.proc_alive(queue.get()) assert not portal.proc_alive(queue.get()) @@ -99,7 +99,7 @@ def inner(ready): ready.wait() worker.kill() assert not worker.running - assert worker.exitcode < 0 + assert abs(worker.exitcode) >= 1 def test_initfn(self): diff --git a/tests/test_server.py b/tests/test_server.py index 32be753..f0a2357 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -199,7 +199,7 @@ def postfn(x): def test_shared_pool(self, repeat, Server): def slow(x): - time.sleep(0.2) + time.sleep(0.5) return x def fast(x): @@ -226,7 +226,7 @@ def fast(x): def test_separate_pools(self, repeat, Server): def slow(x): - time.sleep(0.1) + time.sleep(0.5) return x def fast(x): diff --git a/tests/test_thread.py b/tests/test_thread.py index 1347285..357463c 100644 --- a/tests/test_thread.py +++ b/tests/test_thread.py @@ -91,4 +91,4 @@ def inner(ready): assert not worker.running assert not proc[0].running assert worker.exitcode == 2 - assert proc[0].exitcode < 0 + assert abs(proc[0].exitcode) >= 1