首页 > 解决方案 > psutil杀死错误的进程

问题描述

我的代码有问题,想知道你们是否可以帮助我。这是代码示例:

PID = os.getpid() # Here i get the process id of self.
print(PID) #Here i print the id
pids = [] #Making an array for other ids
for i in range(9999): #Loop through 9999 possible pids
   pids.append(i)# +1 each loop
   print(pids[i]) 
   if pids[i] != PID: #if it is not this pid kill the program.
        try:
            psutil.Process(i).kill()
            time.sleep(1)
        except:
            print("nope")
   else:
        print("Same") # If it is the same pid don't kill it and pass.
        pass

那么问题是什么?

假设我的 PID 是 20。“for”循环到达 20 并说 SAME 并且不会终止程序[太棒了!] 之后它到达 21 并终止进程 20 [它刚刚跳过的主程序,也就是 self. ..]

为什么不这样做就自杀了?如果你有任何想法告诉我。我搜索了谷歌和堆栈溢出,但没有运气。

标签: python-3.xpsutil

解决方案


推荐阅读