Forums

CPU seconds consumed at lower rate in tarpit

Hi there,

Long time user here, I recently deployed an always on task running a simple stock trading script which downloads new data every 5 minutes from an API, checks to see if certain conditions are met for a buy/sell order, executes any orders via the API before saving some data to an SQL database attached to my pythonanywhere account.

I have noticed that each 5 minute block seems to consume around 150 CPU seconds when not running in the tarpit, but when I am inevitably put in the tarpit, that rate seems to drop to around 20 CPU seconds, despite all the same tasks being completed.

I am wondering what explains the difference in CPU second consumption rates and if there is anything I can do to make my code run at the lower rate to avoid entering the tarpit.

Appreciate any insights you are able to provide. Thanks, Matt

Are you sure the same tasks run in the 5 min resolution when it tarpit as when not? When you're in the tarpit your code runs slower. I'd assume that less code is being executed when you're in the tarpit -- maybe you could add some extra logging to confirm that. Also -- how your task is running? Is it in a while loop with some sleeps / checks, or is it restarting each time?

Thanks for your response! Digging deeper, while all tasks are completed within the 5 minute block, the time taken in tarpit is around 3 minutes vs 30 seconds under normal conditions. My code is running in a while loop, which continuously checks to see if 5 minutes have elapsed since the last block. I had not implemented any sleep time in the loop, so it would just continuously check the time. Could it be possible that this would chew up so many CPU seconds? I have added a sleep time of 15 seconds to see if that reduces consumption during normal conditions.

Yes, that will consume an enormous amount of CPU time. A sleep like you have added will reduce the CPU used significantly.