Python Forum
Trying to find a more cpu effecent way of doing this.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to find a more cpu effecent way of doing this.
#1
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.
Reply
#2
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!
Reply
#3
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
Reply
#4
Have you read my first sentence about the indentation of statement 7?
Reply
#5
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.
Reply
#6
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)
Reply
#7
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)
Reply
#8
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!
Reply
#9
I played it on either a DEC PDP-8 or PDP-11.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020