问题复现如下:
>>> a = psutil.pids()
>>> a
[0, 4, 396, 516, 612, 764, 772, 860, 916, 332, 464, 512, 972, 1112, 1164, 1296, 1564, 1784, 1908, 1968, 2064, 2148, 2192, 2280, 2412, 2556, 2564, 2572, 2580, 2588, 2668, 2708, 2788, 2796, 2804, 2872, 2884, 2900, 2460, 3236, 3780, 4376, 5352, 5684, 2684, 2180, 6060, 488, 2616, 1244, 2692, 4356, 5972, 3516, 5960, 1984, 3452, 6496, 6628, 7812, 7872, 7976, 8052, 8076, 8096, 8120, 8148, 8160, 1744, 7508, 7720, 7744, 8072, 2376, 7436, 8220, 9088, 9128, 8508, 8412, 12188, 3484, 3680, 5928, 5348, 7588, 16352, 5872, 604, 15716, 7028, 16168, 12880, 15220, 11432, 14988, 100, 15564, 12856, 16796, 7064, 17356, 16028, 10380, 14980, 9864, 14776, 16064, 17028, 728, 9792, 8024, 15304, 14760, 17196, 13072, 12244, 13712, 9876, 16876, 11956, 6556, 11608, 13080, 16892, 15456, 8156, 11664, 12792, 6248, 14940, 16188, 15324, 1000, 16212, 7752, 13520, 12328, 4724]
>>> type (a)
<class 'list'>
# 这里手工 KILL 掉 14988 进程, 模拟被其它程序关闭....etc
>>> psutil.Process(14988)
Traceback (most recent call last):
File "C:\Python3\lib\site-packages\psutil\
_pswindows.py", line 636, in wrapper
return fun(self, *args, **kwargs)
File "C:\Python3\lib\site-packages\psutil\
_pswindows.py", line 822, in create_time
return cext.proc_create_time(self.pid)
ProcessLookupError: [Errno 3] No such process
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python3\lib\site-packages\psutil\
__init__.py", line 428, in _init
self.create_time()
File "C:\Python3\lib\site-packages\psutil\
__init__.py", line 754, in create_time
self._create_time = self._proc.create_time()
File "C:\Python3\lib\site-packages\psutil\
_pswindows.py", line 641, in wrapper
raise NoSuchProcess(self.pid, self._name)
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=14988)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
psutil.Process(14988)
File "C:\Python3\lib\site-packages\psutil\
__init__.py", line 401, in __init__
self._init(pid)
File "C:\Python3\lib\site-packages\psutil\
__init__.py", line 441, in _init
raise NoSuchProcess(pid, None, msg)
psutil.NoSuchProcess: psutil.NoSuchProcess no process found with pid 14988
>>>