Python Forum
Trying to find a more cpu effecent way of doing this. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to find a more cpu effecent way of doing this. (/thread-6976.html)



Trying to find a more cpu effecent way of doing this. - rawrmonster - Dec-16-2017

def KillProcess(list):
    while(True):
        for program in list:
            for proc in psutil.process_iter():
                if proc.name() == program:
                    proc.kill()
                    time.sleep(3)
This does what I need it to but I am using about 33% of the cpu. I am trying to find different ways of doing this that might be easy on my system.


RE: Trying to find a more cpu effecent way of doing this. - squenson - Dec-16-2017

If your intention is to evaluate the list of programs and kill some of them, then to have a pause of three seconds, your statement 7 is wrongly indented. And you realize that you will never ever leave the while loop, you are like inside the horizon of a black hole, trapped forever!


RE: Trying to find a more cpu effecent way of doing this. - rawrmonster - Dec-16-2017

Yes I know this will never end and it is getting attached to a thread which is why I am looking for this to be more efficient. I am having this function using 33% or more cpu which is why I am trying to get this easier on the processer


RE: Trying to find a more cpu effecent way of doing this. - squenson - Dec-16-2017

Have you read my first sentence about the indentation of statement 7?


RE: Trying to find a more cpu effecent way of doing this. - rawrmonster - Dec-16-2017

Yes I understand that it will kill one program then wait 3 seconds then kill the next in the array. It was a failed attempt to have the program not eating up so much cpu. I figured the time.sleep getting hit so often would cause the cpu to work less.


RE: Trying to find a more cpu effecent way of doing this. - snippsat - Dec-16-2017

Run it a schedule either bye using OS,or example Python job scheduling for humans.
Then sleep(0-0.1% CPU) and only kick in when schedule run function.
Something like this check function every 20-sec untested.
import schedule
import time

def KillProcess(lst):
    for program in lst:
        for proc in psutil.process_iter():
            if proc.name() == program:
                proc.kill()

your_list = [1, 2, 3]
schedule.every(.2).minutes.do(KillProcess, lst)
while True:
    schedule.run_pending()
    time.sleep(1)



RE: Trying to find a more cpu effecent way of doing this. - Larz60+ - Dec-16-2017

Quote:you are like inside the horizon of a black hole, trapped forever!
In a maze of twisty little passages all alike. (From the first adventure game ... Colossal Cave)


RE: Trying to find a more cpu effecent way of doing this. - squenson - Dec-16-2017

Quote:In a maze of twisty little passages all alike. (From the first adventure game ... Colossal Cave)
I am old enough to remember this game, on my TRS-80! (In a few years, I will be old enough to have forgotten it!)

Quote:Yes I understand that it will kill one program then wait 3 seconds then kill the next in the array.
But if there is nothing to kill, your program will again go through the list without waiting three seconds and this is what consumes your CPU cycles.

Just move statement 7 three indents to the left (align it to statement 3) and let us know the results!


RE: Trying to find a more cpu effecent way of doing this. - Larz60+ - Dec-16-2017

I played it on either a DEC PDP-8 or PDP-11.