Skip to content

Commit

Permalink
[Linux] speedup Process.pid() by 20% by reading it from /proc/pid/sta…
Browse files Browse the repository at this point in the history
…t instead of /proc/pid/status
  • Loading branch information
giampaolo committed May 1, 2016
1 parent 1272150 commit db2589e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
- #795: [Windows] new APIs to deal with Windows services: win_service_iter()
and win_service_get().
- #800: [Linux] psutil.virtual_memory() returns a new "shared" memory field.
- XXX: [Linux] speedup /proc parsing:
- Process.ppid() is 20% faster

**Bug fixes**

Expand Down
14 changes: 5 additions & 9 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,15 +1395,11 @@ def num_fds(self):

@wrap_exceptions
def ppid(self):
fpath = "%s/%s/status" % (self._procfs_path, self.pid)
with open_binary(fpath) as f:
for line in f:
if line.startswith(b"PPid:"):
# PPid: nnnn
ppid = int(line.split()[1])
self._ppid = ppid
return ppid
raise NotImplementedError("line 'PPid' not found in %s" % fpath)
with open_binary("%s/%s/stat" % (self._procfs_path, self.pid)) as f:
data = f.read()
# ignore the first two values ("pid (exe)")
data = data[data.rfind(b')') + 2:]
return int(data.split(b' ')[1])

@wrap_exceptions
def uids(self):
Expand Down

0 comments on commit db2589e

Please sign in to comment.