4

I'm not going to provide the code because it's too long. The python script involves executing a lengthy number of commands run in a while loop.

Basic structure

while True:
    The meat goes here
    with the odd if:
        and stuff

Now that I'm finished it, I'm noticing that upon running it, it uses 100% CPU, no exceptions. I'm a beginner and don't really know what to attribute this issue to. I thought that maybe because the script runs indefinitely (until I exit it manually) it might just be taxing on the CPU if it's repeating the loop a number of times a second. I added time.sleep(1) at the bottom of the while to see if that was the issue with no improvements.

Anyone have any ideas? It is quite a long sequence of events, but they are heavily dependent on an if statement which isn't triggered all that often. The 100% CPU usage occurs before that particular if statement is even triggered, so I'm really at a loss.

Edit: forgot to include that it's running in a unix environment (Debian)

4
  • If you already tried adding time.sleep(1) below the while True: line, and it didn't help, then I think we need to see more code before we can help you. Commented Dec 15, 2010 at 12:42
  • 1
    Do you have any continue statements in your code which could be skipping over the sleep (if it's at the bottom of the while loop)? Commented Dec 15, 2010 at 12:44
  • Thanks for all of the answers guys. I have used other scripts in the past that run passively with almost no CPU usage. I'll look into making sure that the time.sleep(1) is in doing its job. Commented Dec 15, 2010 at 13:36
  • I've got it so that the time.sleep(1) is now working - not sure why placing it at the bottom of the loop wasn't doing it. This makes it so that it's using virtually no CPU :) Commented Dec 15, 2010 at 13:46

5 Answers 5

12

Unless there is something to get in the way of the CPU being used (for example, waiting on disk IO or network IO, or pausing execution by sleeping), CPU usage will always be at around 100% while a program is running.

You might want to add in a time.sleep(numberOfSeconds) to your loop if you don't want it to be using 100% CPU all the time, if it's only checking for a certain condition over and over.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bud, this is my first real attempt at making a useful script and hadn't realized that 100% CPU is to be expected without time.sleep()
4

The problem is that you don't have a sleep statement in the loop unless the if function is true, so you code is looping as ridiculous speed taking vast amounts of processor speed.

All you need to do is add an sleep line with 0.1.

time.sleep(0.1)

Comments

3

100% CPU means that script runs well. I don't see any problem. If it prevents other programs to run well, run script under lower priority (nice)

Comments

2

The question is why it should not use 100%. That's the default for anything you write. For it to not use 100% you need to have specific code that sits and waits for something to happen. If you do have that, then the error is in that code.

2 Comments

I thought that by default, Python uses only 1 core of the processor, and you have to do multithreadding or parallel processing to make it use more cores. Is that wrong?
No, but that's 100% of that core.
1

Perhaps there is a break or continue inside the while loop, so that your time.sleep(1) code is skipped. Are you sure the time.sleep(1) part is being executed?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.