导语:
本文主要介绍了关于Python如何判断程序是否运行的相关知识,包括一个实例已在运行,以及java判断程序启动结束这些编程知识,希望对大家有参考作用。
本篇文章介绍了Python判断程序进程是否存在的方法,希望对学习Python的朋友有帮助!
Python如何判断程序是否运行
1、进程名
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
2、进程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 中使用 numpy 进行图像处理09/18
- ♥ python如何求解线性方程组?09/12
- ♥ python如何装饰带参数的装饰器01/05
- ♥ python中的格式化函数是什么?如何使用?10/26
- ♥ 如何使用 cmd 运行 python 脚本10/04
- ♥ python3.7打包成exe需要三步10/27
内容反馈