diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-05-02 05:14:23 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-05-02 05:14:23 +0200 |
commit | 3c14211b58492d27144a40170e4ce0e2f979af44 (patch) | |
tree | b7dd09b110948777d649d767db2a8b3a78990e60 | |
parent | 5aa34c0444d2a8675550df33282ab9b2dd979424 (diff) | |
download | psutil-3c14211b58492d27144a40170e4ce0e2f979af44.tar.gz |
localize variable access for speedup
-rw-r--r-- | psutil/_psposix.py | 47 | ||||
-rwxr-xr-x | psutil/tests/test_linux.py | 1 | ||||
-rwxr-xr-x | psutil/tests/test_process.py | 9 |
3 files changed, 35 insertions, 22 deletions
diff --git a/psutil/_psposix.py b/psutil/_psposix.py index 69219ceb..b7b57477 100644 --- a/psutil/_psposix.py +++ b/psutil/_psposix.py @@ -47,7 +47,12 @@ def pid_exists(pid): return True -def wait_pid(pid, timeout=None, proc_name=None): +def wait_pid(pid, timeout=None, proc_name=None, + _waitpid=os.waitpid, + _timer=getattr(time, 'monotonic', time.time), + _min=min, + _sleep=time.sleep, + _pid_exists=pid_exists): """Wait for a process PID to terminate. If the process terminated normally by calling exit(3) or _exit(2), @@ -65,21 +70,21 @@ def wait_pid(pid, timeout=None, proc_name=None): If *timeout* != None and process is still alive raise TimeoutExpired. timeout=0 is also possible (either return immediately or raise). """ - assert pid > 0, pid - timer = getattr(time, 'monotonic', time.time) + if pid <= 0: + raise ValueError("can't wait for PID 0") # see "man waitpid" interval = 0.0001 - flags = os.WUNTRACED | os.WCONTINUED + flags = 0 if timeout is not None: flags |= os.WNOHANG - stop_at = timer() + timeout + stop_at = _timer() + timeout def sleep(interval): # Sleep for some time and return a new increased interval. if timeout is not None: - if timer() >= stop_at: + if _timer() >= stop_at: raise TimeoutExpired(timeout, pid=pid, name=proc_name) - time.sleep(interval) - return min(interval * 2, 0.04) + _sleep(interval) + return _min(interval * 2, 0.04) # See: https://linux.die.net/man/2/waitpid while True: @@ -94,7 +99,7 @@ def wait_pid(pid, timeout=None, proc_name=None): # - PID never existed in the first place # In both cases we'll eventually return None as we # can't determine its exit status code. - while pid_exists(pid): + while _pid_exists(pid): interval = sleep(interval) return else: @@ -111,18 +116,18 @@ def wait_pid(pid, timeout=None, proc_name=None): # Process exited due to a signal != SIGSTOP. Return the # negative value of that signal. return -os.WTERMSIG(status) - elif os.WIFSTOPPED(status): - # Process was stopped via SIGSTOP or is being traced, and - # waitpid() was called with WUNTRACED flag. Anyway, it's - # still alive. From now on waitpid() will keep returning - # (0, 0) until the process state doesn't change. - interval = sleep(interval) - continue - elif os.WIFCONTINUED(status): - # Process was resumed via SIGCONT and waitpid() was called - # with WCONTINUED flag. - interval = sleep(interval) - continue + # elif os.WIFSTOPPED(status): + # # Process was stopped via SIGSTOP or is being traced, and + # # waitpid() was called with WUNTRACED flag. Anyway, it's + # # still alive. From now on waitpid() will keep returning + # # (0, 0) until the process state doesn't change. + # interval = sleep(interval) + # continue + # elif os.WIFCONTINUED(status): + # # Process was resumed via SIGCONT and waitpid() was called + # # with WCONTINUED flag. + # interval = sleep(interval) + # continue else: # Should never happen. raise ValueError("unknown process exit status %r" % status) diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index fcc9d5db..709c77ba 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -1362,6 +1362,7 @@ class TestMisc(PsutilTestCase): finally: psutil.PROCFS_PATH = "/proc" + @retry_on_failure() def test_issue_687(self): # In case of thread ID: # - pid_exists() is supposed to return False diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index e0eb2443..3caeb420 100755 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -1371,8 +1371,15 @@ class TestProcess(PsutilTestCase): self.assertRaises(psutil.NoSuchProcess, psutil.Process, 0) return - # test all methods p = psutil.Process(0) + self.assertRaises(ValueError, p.wait) + self.assertRaises(ValueError, p.terminate) + self.assertRaises(ValueError, p.suspend) + self.assertRaises(ValueError, p.resume) + self.assertRaises(ValueError, p.kill) + self.assertRaises(ValueError, p.send_signal, signal.SIGTERM) + + # test all methods for name in psutil._as_dict_attrnames: if name == 'pid': continue |