Skip to content

Commit

Permalink
#1083 / #1084: implement linux-specific ppid_map() function speending…
Browse files Browse the repository at this point in the history
… things up from 2x to 2.4x
  • Loading branch information
giampaolo committed Nov 30, 2017
1 parent fe85bdf commit 78f8cbc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- 1173_: introduced PSUTIL_DEBUG environment variable which can be set in order
to print useful debug messages on stderr (useful in case of nasty errors).
- 1177_: added support for sensors_battery() on OSX. (patch by Arnon Yaari)
- 1183_: Process.children() is around 2.2x faster on UNIX.
- 1183_: Process.children() is 2x faster on UNIX and 2.4x faster on Linux.

**Bug fixes**

Expand Down
4 changes: 2 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@


if hasattr(_psplatform, 'ppid_map'):
# Windows only (C).
# Faster version (Windows and Linux).
_ppid_map = _psplatform.ppid_map
else:
def _ppid_map():
"""Obtain a {pid: ppid, ...} dict for all running processes in
"""Return a {pid: ppid, ...} dict for all running processes in
one shot. Used to speed up Process.children().
"""
ret = {}
Expand Down
21 changes: 21 additions & 0 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,27 @@ def pid_exists(pid):
return pid in pids()


def ppid_map():
"""Obtain a {pid: ppid, ...} dict for all running processes in
one shot. Used to speed up Process.children().
"""
ret = {}
procfs_path = get_procfs_path()
for pid in pids():
try:
with open_binary("%s/%s/stat" % (procfs_path, pid)) as f:
data = f.read()
except EnvironmentError as err:
if err.errno not in (errno.ENOENT, errno.EPERM, errno.EACCES):
raise
else:
rpar = data.rfind(b')')
dset = data[rpar + 2:].split()
ppid = int(dset[1])
ret[pid] = ppid
return ret


def wrap_exceptions(fun):
"""Decorator which translates bare OSError and IOError exceptions
into NoSuchProcess and AccessDenied.
Expand Down

0 comments on commit 78f8cbc

Please sign in to comment.