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 并说 SAME 并且不会终止程序[太棒了!] 之后它到达 21 并终止进程 20 [它刚刚跳过的主程序,也就是 self. ..]
为什么不这样做就自杀了?如果你有任何想法告诉我。我搜索了谷歌和堆栈溢出,但没有运气。
解决方案
推荐阅读
- tableau-api - 一旦对应于具有多个级别的变量,如何选择员工ID?在 Tableau 中?
- java - 无法将 Android Studio 与 Oracle Database 19c 连接
- java - 客户端健康检查:如果代理关闭,则检查消费者/生产者
- javascript - 以隐藏模式暂停/恢复 YouTube
- php - Directus:如何访问 PHP 中的集合项目以创建社交媒体的 Meta 标签?
- javascript - 通过定位“数据”属性在重新加载后将类添加到 div
- javascript - 在 MaterialUI 和 React.js 中为 Autocomplete 分配默认值
- javascript - 使用 Axios 将 API 响应存储到 Node.js 中的变量中
- typescript - NestJs 全局 HttpExceptionFilter 无法正常工作
- php - 我想从 Laravel 的存储文件夹中下载多个文件