Forums

CPU seconds seem to keep being used even when no tasks running

Been trying (and failing) to get an always-on task going. It seems to not run at all, until I press the stop button, then it runs once. This is possibly due to my inexperience with python rather than a problem with pythonanywhere.

But it still uses loads of CPU seconds and seems to continue using them even after process is stopped - is this normal? I stopped everything last night and went to bed and woke up and I've apparently used about 800 CPU seconds, but it 100% was stopped before I went to bed. Had still managed to rack up over 100 cpu seconds trying and failing to execute my script which seemed like quite a few, but 800s for doing nothing I don't understand. Am I missing something ?

That sounds like it might be starting, then immediately exiting, or something like that -- if something crashes on startup, then the system will keep trying to start it -- and that can use up CPU seconds over time.

Is there anything in the logs for the task? If there's nothing obvious, can I take a look at your code? We can see it from our admin interface, but we always ask for permission first.

No the logs just have one line "starting up" or something like that. Yes please do feel free to look at the code, thank you

OK, I think I've tracked down what's going on. The CPU usage after you stopped the task wasn't from the task itself -- it was from a Jupyter notebook. They consume CPU at a rate of about a second a minute, even when idle, and one has been running in your account. You can shut it down totally by using the "Running processes" table on the "Consoles" page to kill all processes called things like "jupyter", "kernel" or "sudospawner". That would explain about 90% of the CPU usage that's showing up against your account right now.

Regarding the high CPU usage when you were testing it, your code is like this (I've changed a couple of function/variable to protect your privacy):

time_to_wait_to_do_something = 30

#...

while True:
    if round(time.time()) % time_to_wait_to_do_something == 0:
        do_the_thing()
        time.sleep(1)

That is essentially using up CPU power as fast as it can most of the time. To see why, consider what happens if the test fails -- it just goes immediately around the loop again, without sleeping. So most of the time it's in a busy wait -- that is, every 30 seconds it sleeps for a second, but the rest of the time it might as well be

while True:
    pass

Which will consume CPU at a rate of one second per second.

If you move the time.sleep(1) outside the if statement, that should sort out the task's CPU usage.

Finally, regarding getting it to actually do what you want it to do :-) I'd suggest adding in a bit of logging to see exactly what's happening in that loop. Combined with the moved time.sleep() statement, something like this should give you an idea about what's going on:

time_to_wait_to_do_something = 30

#...

print("Starting loop....")
while True:
    if round(time.time()) % time_to_wait_to_do_something == 0:
        print("Doing the thing")
        do_the_thing()
    time.sleep(1)

That's really helpful, thank you for going into so much detail I really appreciate it

After having implemented your suggestions, the problem with it consuming cpu seconds has been fixed (or perhaps replaced by a different problem). It now consumes no seconds at all, but still get stuck on "starting". The log only shows "- Task preparing to start"

I'm not sure if its a problem with my script. It works fine when I run it on digital ocean so I don't think it is a problem with my script, but maybe I am missing something with how to set it up for pythonanywhere? I want to run it on pythonanywhere because I don't trust the screener on DO to not break at any point

When I go to your logs, it seems like there are a bunch of errors. That log does necessarily update immediately, especially if it is not flushed. Your task is stuck on starting basically because every time it starts it immediately errors and dies.

hmm thanks for the info. I'll keep tinkering

My script is exactly the same as when I run it on digital ocean but it runs fine there. Except I am using a screener which I don't trust to keep running. I know this must be frustrating to get a question from a hobbyist, but why would my script not even start on pythonanywhere if it runs fine on digital ocean? If that's too in depth to reply please just point me at some documentation to get started. I have checked and all the required modules for my script are installed on my python anywhere account, I can't see any reason why the script wouldn't run. Thanks

Make sure you've left enough time for the task to start (it can take a few minutes) and then look at the logs to see what the problem may be.