小编给大家分享一下判断Python程序有没有运行的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

Python如何判断程序是否运行

1、进程名

importpsutildefjudgeprocess(processname):pl=psutil.pids()forpidinpl:ifpsutil.Process(pid).name()==processname:print(pid)breakelse:print("notfound")ifjudgeprocess('notepad++.exe')==0:print('success')else:pass

2、进程ID

importerrnoimportosimportsysdefpid_exists(pid):"""Checkwhetherpidexistsinthecurrentprocesstable.UNIXonly."""ifpid<0:returnFalseifpid==0:#Accordingto"man2kill"PID0referstoeveryprocess#intheprocessgroupofthecallingprocess.#Oncertainsystems0isavalidPIDbutwehavenoway#toknowthatinaportablefashion.raiseValueError('invalidPID0')try:os.kill(pid,0)exceptOSErroraserr:iferr.errno==errno.ESRCH:#ESRCH==NosuchprocessreturnFalseeliferr.errno==errno.EPERM:#EPERMclearlymeansthere'saprocesstodenyaccesstoreturnTrueelse:#Accordingto"man2kill"possibleerrorvaluesare#(EINVAL,EPERM,ESRCH)raiseelse:returnTrue

看完了这篇文章,相信你对判断Python程序有没有运行的方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!