Forums

Is threading CPU intensive?

Hey,

So I wrote a script to monitor and alert me if a site goes down, it has plenty of sleep() time in it, which I read doesn't count towards CPU usage in another forum post but having run it for the past half hour, doing maybe 10-15 checks against sites I'm wanting to monitor, it has already used 60% of my daily CPU usage.

I wrote a simpler version first and ran this for hours (24 hours) and it never got more that a few %. the only real difference with my code is the adding of threading.

Is it a glitch or is threading a no-no?

I'm not aware of anything about threading that would make it more CPU intensive and sleep should also not be using any CPU. However, threading is one of the harder things in programming so it's much easier to make a mistake with threads than with many other things and, in terms of CPU use, threads will magnify the mistake (by making many copies of it).

I just can't see how this is so intensive - code seen here http://www.craigaddyman.com/python-script-to-monitor-site-up-time/

Appreciate you taking a look

Is it functioning OK - do you get the expected 'prints' and 'emails'?

I agree that there's nothing obviously CPU-intensive in that code, but it is hard to reason about multi-threaded code and there could be some kind of busy wait case there that's not immediately obvious. Perhaps adding more logging so that you can see how frequently it's going through each of those loops would uncover something?

hey chaps.

Thanks for taking a look, I've just figured out what was causing the issue, basically the second function, site_down(), was checking an empty dictionary (waiting for something to be added to it), because none of the conditions were met (because it was empty), it was looping round without any sleep. I moved my sleep function to directly after the while loop (not updated on the link above yet) and that sorted the issue.

Ah, that makes sense. Thanks for letting us know!