导语:
本文主要介绍了关于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 写了一个「表情包」,赚了250万02/20
- ♥ 如何查看python工具包12/21
- ♥ 如何用 Python 编写一副扑克牌?10/08
- ♥ 什么是 Python IDE?哪个更好用?11/02
- ♥ Python中的匿名函数是什么11/08
- ♥ python是脚本语言吗?01/10
内容反馈