Forums

Minimise CPU time

Hey Guys,

I want to run a script that sleeps for most of the day but wakes up from time to time to complete a task. These tasks are not intensive at all - however the main thing is that the code has to wake up.

When using the time.sleep function, I found that it used a lot of CPU time - time.sleep(100) used 0.5 CPU seconds - surely if the code is sleeping, then it shouldn't use any?

Apologies if it's been posted before, but has anyone got any better solutions?

Thanks!

We have scheduled tasks for just this purpose. We'll take care of "waking up" your process and you just need to write the code that you want to run.

I want to schedule it on a minutely basis, though - and still use the variables in between scheduled runnings.

If you need the variables between runs of a scheduled task, you will need to write them to disk or database so that the next run can read them in again. For minutely scheduling and keeping variables between runs, use an always on task with a while True loop and a time.sleep to pause between each loop. Be aware that, if your always on task exits and has to be restarted, the variables in the script will be reset unless you have a way of saving them.