Skip to content

Commit

Permalink
Improved get_child_processes() which now will find child processes re…
Browse files Browse the repository at this point in the history
…cursively
  • Loading branch information
PalNilsson committed Sep 29, 2023
1 parent 2d42a93 commit ad4ad15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion PILOTVERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.9.1
3.6.9.2
2 changes: 1 addition & 1 deletion pilot/util/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
RELEASE = '3' # released number should be fixed at 3 for Pilot 3
VERSION = '6' # version number is '1' for first release, '0' until then, increased for bigger updates
REVISION = '9' # revision number should be reset to '0' for every new version release, increased for small updates
BUILD = '1' # build number should be reset to '1' for every new development cycle
BUILD = '2' # build number should be reset to '1' for every new development cycle

SUCCESS = 0
FAILURE = 1
Expand Down
39 changes: 26 additions & 13 deletions pilot/util/psutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,42 @@ def get_child_processes(parent_pid):
if not _is_psutil_available:
logger.warning('get_child_processes(): psutil not available - using legacy code as a fallback')
return get_child_processes_legacy(parent_pid)
else:
return get_all_descendant_processes(parent_pid)

child_processes = []

# Iterate through all running processes
for process in psutil.process_iter(attrs=['pid', 'ppid']):
try:
process_info = process.info()
pid = process_info['pid']
ppid = process_info['ppid']
def get_all_descendant_processes(parent_pid):
"""
Recursively find child processes using the given parent pid as a starting point.
# Check if the process has the specified parent PID
if ppid == parent_pid:
child_processes.append(pid)
:param parent_pid: parent process id (int)
:return: descendant process ids (list).
"""

def find_descendant_processes(pid):
try:
descendants = []
for process in psutil.process_iter(attrs=['pid', 'ppid']):
process_info = process.info
child_pid = process_info['pid']
ppid = process_info['ppid']
if ppid == pid:
descendants.append(child_pid)
descendants.extend(find_descendant_processes(child_pid))
return descendants
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return []

return child_processes
all_descendant_processes = find_descendant_processes(parent_pid)
return all_descendant_processes


def get_child_processes_legacy(parent_pid):
"""
Return a list of all child processes belonging to the same parent process id.
Using a fallback to /proc/{pid} in case psutil is not available.
Note: this approach is not efficient if one is to find all child processes using
the parent pid as a starting point. Better to use a recursive function using psutil.
This method should be removed once psutil is available everywhere.
:param parent_pid: parent process id (int)
:return: child processes (list).
Expand Down

0 comments on commit ad4ad15

Please sign in to comment.