Psutil杀死错误的进程

时间:2019-03-06 19:16:17

标签: python-3.x 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,并表示相同,并且不会终止程序[太棒了!] 之后,它进入21并杀死进程20 [它只是跳过了自己的主程序...]

为什么不这样做会自杀? 如果您有任何想法请告诉我。我在Google周围搜索并且堆栈溢出,但是没有运气。

0 个答案:

没有答案
相关问题