Skip to content

Commit c5358e5

Browse files
committed
fix: rewritten __poll() using psutil
1 parent 6cac2c4 commit c5358e5

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/DIRAC/Core/Utilities/Subprocess.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ def __init__(self, timeout=False, bufferLimit=52428800):
162162

163163
self.child = None
164164
self.childPID = 0
165-
self.childKilled = False
166165
self.callback = None
167166
self.bufferList = []
168167
self.cmdSeq = []
@@ -252,13 +251,20 @@ def __killProcess(self, process):
252251
return psutil.wait_procs([process])
253252

254253
def __poll(self, pid):
255-
"""wait for :pid:"""
254+
"""Non-blocking check of whether process `pid` is still alive.
255+
Returns:
256+
- (0, 0) if process is still running (like os.waitpid(pid, os.WNOHANG))
257+
- (pid, exitcode) if process has terminated
258+
- None if process info cannot be retrieved
259+
"""
256260
try:
257-
return os.waitpid(pid, os.WNOHANG)
258-
except os.error:
259-
if self.childKilled:
260-
return False
261-
return None
261+
p = psutil.Process(pid)
262+
exitcode = p.wait(timeout=0)
263+
return (pid, exitcode) # exited
264+
except psutil.TimeoutExpired:
265+
return (0, 0) # still running
266+
except psutil.NoSuchProcess:
267+
return (pid, 0)
262268

263269
def killChild(self, recursive=True):
264270
"""Kills a process tree (including children) with signal SIGKILL.

0 commit comments

Comments
 (0)