python3判断进程是否存在的方法
这篇文章将为大家详细讲解有关python3判断进程是否存在的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
通过进程名判断
importpsutildefjudgeprocess(processname):pl=psutil.pids()forpidinpl:ifpsutil.Process(pid).name()==processname:print(pid)breakelse:print("notfound")ifjudgeprocess('notepad++.exe')==0:print('success')else:pass
通过进程ID判断
importerrnoimportosimportsysdefpid_exists(pid):"""Checkwhetherpidexistsinthecurrentprocesstable.UNIXonly."""ifpid<0:returnFalseifpid==0:#Accordingto"man2kill"PID0referstoeveryprocess#intheprocessgroupofthecallingprocess.#Oncertainsystems0isavalidPIDbutwehavenoway#toknowthatinaportablefashion.raiseValueError('invalidPID0')try:os.kill(pid,0)exceptOSErroraserr:iferr.errno==errno.ESRCH:#ESRCH==NosuchprocessreturnFalseeliferr.errno==errno.EPERM:#EPERMclearlymeansthere'saprocesstodenyaccesstoreturnTrueelse:#Accordingto"man2kill"possibleerrorvaluesare#(EINVAL,EPERM,ESRCH)raiseelse:returnTrue
关于python3判断进程是否存在的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。