python3判断进程是否存在的方法-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

这篇文章将为大家详细讲解有关python3判断进程是否存在的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
通过进程名判断
import psutil
def judgeprocess(processname):
pl = psutil.pids()
for pid in pl:
if psutil.Process(pid).name() == processname:
print(pid)
break
else:
print("not found")
if judgeprocess('notepad++.exe') == 0:
print('success')
else:
pass通过进程ID判断
import errno
import os
import sys
def pid_exists(pid):
"""Check whether pid exists in the current process table.
UNIX only.
"""
if pid < 0:
return False
if pid == 0:
# According to "man 2 kill" PID 0 refers to every process
# in the process group of the calling process.
# On certain systems 0 is a valid PID but we have no way
# to know that in a portable fashion.
raise ValueError('invalid PID 0')
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# ESRCH == No such process
return False
elif err.errno == errno.EPERM:
# EPERM clearly means there's a process to deny access to
return True
else:
# According to "man 2 kill" possible error values are
# (EINVAL, EPERM, ESRCH)
raise
else:
return True关于python3判断进程是否存在的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前标题:python3判断进程是否存在的方法-创新互联
标题来源:http://www.yantingwz.cn/article/iohhs.html
其他资讯
- A5好推宝:模仿中的营销学问同消费场景中也能学到的运营知识
- 短视频营销的主要类型有哪些-短视频自动配乐:让你的视频与众不同!
- PHP7打开Opcache的方法
- asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- 域名怎么对应linux的项目 linux 域名
- 路由器的web管理界面 路由器web管理界面映射到外网
- ios开发者升级正式版 ios开发版升级正式版
- 什么是软文网站,解析软文网站的作用和功能
- 为什么word做的图片
- Linux网络命令大全与域名分析:从零开始掌握网络接口


咨询
建站咨询
