mercredi 28 août 2019

If condition loop

I am trying to run a command to stop an application and then use the psutil to check if all the associated processes are killed. If not, wait for 60 seconds. The code is:

import os, psutil, time

os.system("App -STOP")

for p in psutil.process_iter():
    x = p.as_dict(attrs=['pid', 'name', 'username'])
    if 'as_' in x.get('name') or 'AP_' in x.get('name'):
           time.sleep(60)

If the "as_" process exists, the the loop sleeps for 60 seconds. But once the 60 seconds is complete, the "for" loop would go on to check the other processes. I would like to know a way I can check for the "as_" again and again, and sleep for 60 seconds every time if it is still running (sounds like a bummer, but the process dies for sure in a few minutes). Once the process dies, only then loop goes on to check for other processes. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire