正在執行的程式稱為行程。進程可以是當前作業系統上運行的應用程序,也可以是與作業系統相關的應用程式。如果一個應用程式與作業系統相關,它首先會創建一個進程來執行自己。
其他應用程式依賴作業系統服務來執行。大多數應用程式是作業系統服務以及維護作業系統、軟體和硬體的後台應用程式。
在 python 中,我們有不同的方法來檢查應用程式是否開啟。讓我們一一詳細了解它們。
使用 psutil.process_iter() 函數
psutil 是 python 中的一個模組,它為使用者提供一個介面來檢索正在運行的進程和系統利用率的資訊。可用於Linux、windows、macOs、solaris、AIX等主流作業系統以及API平台。
psutil 模組的 process_iter() 函數幫助我們檢索有關正在運行的進程的信息,例如進程名稱、進程 ID、CPU 使用情況、記憶體使用情況等。它還提供有關係統利用率的信息,例如磁碟使用情況、網路使用情況等。
範例
在此範例中,我們試圖找出名為「Chrome.exe」的進程目前是否正在我們的系統中執行。
import psutil def check_if_process_running(process_name): for process in psutil.process_iter(['name']): if process.info['name'] == process_name: return True return False check_if_process_running("Chrome.exe")
輸出
False
範例
這是 psutil 模組的 process_iter() 函數的另一個範例,它提供了進程的詳細資訊。
import psutil processes = psutil.process_iter() for process in processes: print(f"Process name: {process.name()} | PID: {process.pid}") cpu_percent = psutil.cpu_percent() print(f"CPU usage: {cpu_percent}%") memory_usage = psutil.virtual_memory() print(f"Total memory: {memory_usage.total / 1024 / 1024:.2f} MB") print(f"Available memory: {memory_usage.available / 1024 / 1024:.2f} MB") print(f"Memory usage: {memory_usage.percent}%")
輸出
以下是 process_iter() 的輸出,它提供了有關應用程式的全部資訊。
Process name: chrome.exe | PID: 15964 Process name: chrome.exe | PID: 16876 CPU usage: 10.6% Total memory: 12152.65 MB Available memory: 5849.83 MB Memory usage: 51.9%
使用子流程模組
子進程模組是檢查應用程式是否正在運行或停止的另一種方法。使用 subprocess 模組,我們可以從目前的 Python 程式啟動一個新的應用程式。我們可以使用 check_output() 方法來取得程式、指令的輸出。
範例
在下面的範例中,我們嘗試使用 check_output() 函數來驗證應用程式是否開啟 –
import subprocess def is_process_running(process_name): cmd = 'tasklist /fi "imagename eq {}"'.format(process_name) output = subprocess.check_output(cmd, shell=True).decode() if process_name.lower() in output.lower(): return True else: return False is_process_running("chrome.exe")
輸出
True
使用wmi模組
Windows Management Instrumentation 是 Windows 作業系統中的一組工具,可讓管理員管理遠端和本機。
在Python中,我們有wmi模組,它可以幫助我們檢查應用程式是否正在運行。以下程式碼用於在python環境中安裝wmi。
pip install wmi
範例
在此範例中,我們將應用程式名稱作為輸入參數傳遞給 wmi 模組的 WMI() 函數,以檢索具有進程 ID 的應用程式的狀態。
import wmi f = wmi.WMI() for process in f.Win32_Process(): print(f"{process.ProcessId:>5} {process.Name}")
輸出
下面是wmi模組的WMI()函數的輸出。
0 System Idle Process 4 System 124 Registry 524 smss.exe 752 csrss.exe 868 csrss.exe 888 wininit.exe 940 services.exe 960 lsass.exe 320 winlogon.exe 980 svchost.exe 1048 fontdrvhost.exe 1056 fontdrvhost.exe 1144 WUDFHost.exe 1180 svchost.exe 1268 svchost.exe 1292 WUDFHost.exe 1396 svchost.exe 1404 svchost.exe 1412 svchost.exe 1528 svchost.exe 1640 dwm.exe 1660 svchost.exe
以上是如何在Python中檢查應用程式是否開啟?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用