Home > Backend Development > Python Tutorial > How to check if application is open in Python?

How to check if application is open in Python?

王林
Release: 2023-08-26 18:49:05
forward
1544 people have browsed it

How to check if application is open in Python?

The program being executed is called a process. A process can be an application running on the current operating system or an application related to the operating system. If an application is tied to the operating system, it first creates a process to execute itself.

Other applications rely on operating system services for execution. Most applications are operating system services and background applications that maintain the operating system, software, and hardware.

In python we have different ways to check if the application is open or not. Let’s learn about them in detail one by one.

Use psutil.process_iter() function

psutil is a module in python that provides users with an interface to retrieve information about running processes and system utilization. It can be used on mainstream operating systems and API platforms such as Linux, windows, macOs, solaris, and AIX.

The process_iter() function of the psutil module helps us retrieve information about the running process, such as process name, process ID, CPU usage, memory usage, etc. It also provides information about system utilization such as disk usage, network usage, etc.

Example

In this example, we are trying to find out if a process named "Chrome.exe" is currently running on our system.

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")
Copy after login

Output

False
Copy after login

Example

This is another example of the process_iter() function of the psutil module, which provides details of a process.

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}%")
Copy after login

Output

The following is the output of process_iter(), which provides full information about the application.

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%
Copy after login

Use subprocess module

The subprocess module is another way to check if an application is running or stopped. Using the subprocess module, we can start a new application from the current Python program. We can use the check_output() method to obtain the output of programs and commands.

Example

In the example below, we try to use the check_output() function to verify that the application is open –

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")
Copy after login

Output

True
Copy after login

Use wmi module

Windows Management Instrumentation is a set of tools in the Windows operating system that allows administrators to manage remote and local computers.

In Python, we have the wmi module, which helps us check whether the application is running. The following code is used to install wmi in a python environment.

pip install wmi
Copy after login

Example

In this example, we pass the application name as an input parameter to the WMI() function of the wmi module to retrieve the status of the application with the process ID.

import wmi
f = wmi.WMI()
for process in f.Win32_Process():
    print(f"{process.ProcessId:>5} {process.Name}")
Copy after login

Output

The following is the output of the WMI() function of the wmi module.

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
Copy after login

The above is the detailed content of How to check if application is open in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template