判断Python程序有没有运行的方法-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!
站在用户的角度思考问题,与客户深入沟通,找到新建网站设计与新建网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名申请、虚拟主机、企业邮箱。业务覆盖新建地区。小编给大家分享一下判断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程序有没有运行的方法-创新互联
文章URL:http://myzitong.com/article/gpdhe.html