导语:
本文主要介绍了关于python3怎么判断进程是否存在的相关知识,包括python结束进程,以及python判断元素是否在列表中这些编程知识,希望对大家有参考作用。
通过进程名判断
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
推荐学习《
》!
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python中的变量是一个对象11/30
- ♥ 如何在python中显示map函数的结果11/16
- ♥ 如何快速为python代码添加注释09/14
- ♥ python concat函数的用途是什么?09/15
- ♥ 两种翻转python字符串的方法10/29
- ♥ 如何在python中遍历字典08/30
内容反馈