判断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程序有没有运行的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
当前文章:判断Python程序有没有运行的方法
标题路径:http://cdiso.cn/article/pgeips.html