-1

i want to check every 500 milliseconds if a process/application is running (Windows 10). The code should be very fast and resource efficient!

My Code is this but how to build the 500 milliseconds in. Is psutil the fastest and best way? Thank You.

import psutil

for p in psutil.process_iter(attrs=['pid', 'name']):

if "excel.exe" in (p.info['name']).lower():
    print("Application is running", (p.info['name']).lower())
else:
    print("Application is not Running")
gerrard87
  • 45
  • 5
  • 1
    `time.sleep(0.5)` ? – Mahrkeenerh Oct 14 '21 at 18:38
  • 1
    That, and slap a `while True:` around it all. – cadolphs Oct 14 '21 at 18:40
  • ok but my exe process will not be found. It says its not running but i checked it, there is no naming error. With this code its working but very slow... import wmi f = wmi.WMI() flag = 0 # Iterating through all the running processes for process in f.Win32_Process(): if "Digitale_Ablegeschablone.exe" == process.Name: print("Application is Running") flag = 1 break if flag == 0: print("Application is not Running") – gerrard87 Oct 14 '21 at 18:45

2 Answers2

1

First of all, psutil is a pretty good library. It has C Bindings so you won't be able to get much faster.

import psutil
import time


def print_app():
    present = False

    for p in psutil.process_iter(attrs=['pid', 'name']):
        if "excel.exe" in (p.info['name']).lower():
            present = True

    print(f"Application is {'' if present else 'not'} present")

start_time = time.time()
print_app()
print("--- %s seconds ---" % (time.time() - start_time))

You can know how much time it takes. 0.06sec for me.

if you want to exec this every 0.5s you can simply put a time.sleep because 0.5 >> 0.06.

You can then write this kind of code:

import psutil
import time


def print_app():
    present = False

    for p in psutil.process_iter(attrs=['pid', 'name']):
        if "excel.exe" in (p.info['name']).lower():
            present = True

    print(f"Application is {'' if present else 'not'} present")

while True:
    print_app()
    sleep(0.5)

PS: I changed your code to check if your app was running without printing it. This makes the code faster because print takes a bit of time.

Teddy
  • 19
  • 3
  • thanks it works great for the example excel.exe but for my python executable its not working, it says application is not present but its running (its visible in taskmgr). Any idea why? – gerrard87 Oct 14 '21 at 19:03
  • 1
    If you use `python.exe` as a filter, this won't work. Indeed, each time you'll run it, the python executable will be found because you run your the script watching for process in python itself. – Teddy Oct 14 '21 at 19:14
  • 1
    If use want to differ 2 process for the same executable you will need to add some `attrs` such as `cmdline` – Teddy Oct 14 '21 at 19:15
1

How about doing it like this:

import psutil
import time


def running(pname):
    pname = pname.lower()
    for p in psutil.process_iter(attrs=['name']):
        if pname in p.info['name'].lower():
            print(f'{pname} is running')
            return # early return
    print(f'{pname} is not running')


while True:
    running('excel.exe')
    time.sleep(0.5)
  • how must the code look like if i want the start a other application when excel.exe is not running. I mean its a while True so it starts every 0.5 seconds the new application then or? – gerrard87 Oct 14 '21 at 19:44