Skip to content

Commit

Permalink
More accurate running property for Process
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Sep 11, 2024
1 parent c359b54 commit 45efa95
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion portal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '3.1.12'
__version__ = '3.2.0'

import multiprocessing as mp
try:
Expand Down
12 changes: 9 additions & 3 deletions portal/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ def pid(self):
def running(self):
if not self.started:
return False
return self.process.is_alive()
if not self.process.is_alive():
return False
try:
if psutil.Process(self.pid).status() == psutil.STATUS_ZOMBIE:
return False
except psutil.NoSuchProcess:
return False
return True

@property
def exitcode(self):
Expand All @@ -76,8 +83,7 @@ def join(self, timeout=None):

def kill(self, timeout=1):
try:
proc = psutil.Process(self.pid)
tree = [proc] + list(proc.children(recursive=True))
tree = list(psutil.Process(self.pid).children(recursive=True))
except psutil.NoSuchProcess:
tree = []
self.process.terminate()
Expand Down
3 changes: 2 additions & 1 deletion portal/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def join(self, timeout=None):
self.thread.join(timeout)
return self

def kill(self, timeout=3):
def kill(self, timeout=1):
utils.kill_threads(self.thread, timeout)
[x.kill(0.2) for x in contextlib.context.get_children(self.ident)]
return self

def __repr__(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def fn():
worker = portal.Process(fn, start=True)
worker.kill()
assert not worker.running
assert worker.exitcode == -15
assert worker.exitcode < 0

@pytest.mark.parametrize('repeat', range(5))
def test_kill_with_subproc(self, repeat):
Expand All @@ -62,7 +62,7 @@ def inner(ready):
ready.acquire()
worker.kill()
assert not worker.running
assert worker.exitcode == -15
assert worker.exitcode < 0

@pytest.mark.parametrize('repeat', range(5))
def test_kill_with_subthread(self, repeat):
Expand All @@ -79,7 +79,7 @@ def inner(ready):
ready.wait()
worker.kill()
assert not worker.running
assert worker.exitcode == -15
assert worker.exitcode < 0

def test_initfn(self):
def init():
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 == -15
assert proc[0].exitcode < 0

0 comments on commit 45efa95

Please sign in to comment.