python3判断进程是否存在的方法

  介绍

这篇文章将为大家详细讲解有关python3判断进程是否存在的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

通过进程名判断

import  psutil   ,   def  judgeprocess (processname):   ,,,pl =, psutil.pids ()   ,,,for  pid  pl:拷贝   ,,,,,,,if  psutil.Process (pid) . name (),==, processname:   ,,,,,,,,,,,印刷(pid)   ,,,,,,,,,,,休息   ,,,:   ,,,,,,,印刷(“not  found")   ,,,,,,,   if  judgeprocess (& # 39; notepad++ . exe # 39;),==, 0:   ,,,print(& # 39;成功# 39;)   其他:   ,,,通过

,通过进程ID判断

import  errno   import 操作系统   import 系统   ,   ,   ,   def  pid_exists (pid):   ,,,“““Check  whether  pid  exists 拷贝,current  process 表。   ,,UNIX 只有。   ,,,,,,   ,,,if  pid  & lt;, 0:   ,,,,,,,return 错误的   ,,,if  pid ==, 0:   ,,,,,,,#,According 用“man  2, kill", PID  0, refers 用every 过程   ,,,,,,,#,拷贝,process  group  of 从而calling 过程。   ,,,,,,,#,提醒certain  systems  0, is  a  valid  PID  but  have  no 我方表示歉意   ,,,,,,,#,用知道that 拷贝a  portable 时尚。   ,,,,,,,raise  ValueError (& # 39; invalid  PID  0 & # 39;)   ,,,试一试:   ,,,,,,,os.kill (pid, 0)   ,,,except  OSError  as 错:   ,,,,,,,if  err.errno ==, errno.ESRCH:   ,,,,,,,,,,,#,ESRCH ==, No  such 过程   ,,,,,,,,,,,return 错误的   ,,,,,,,elif  err.errno ==, errno.EPERM:   ,,,,,,,,,,,#,EPERM  clearly  means 还有# 39;s  a  process 用deny  access    ,,,,,,,,,,,return 真实的   ,,,,,,,其他的:   ,,,,,,,,,,,#,According 用“man  2, kill" possible  error  values    ,,,,,,,,,,,#,(EINVAL, EPERM,, ESRCH)   ,,,,,,,,,,,   ,,,:   ,,,,,,,return 真正的

关于python3判断进程是否存在的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。

python3判断进程是否存在的方法