您好,登錄后才能下訂單哦!
小編給大家分享一下判斷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程序有沒有運行的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。