Skip to content

Commit

Permalink
Add warning when using native Python threads
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 12, 2024
1 parent b119cac commit 0a7312d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
8 changes: 7 additions & 1 deletion portal/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import threading
import traceback
import warnings

import cloudpickle
import psutil
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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())

Expand All @@ -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):

Expand Down
4 changes: 2 additions & 2 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0a7312d

Please sign in to comment.