control CPU usage

Dave Angel davea at
Sun Sep 20 12:24:53 CEST 2009

Jiang Fung Wong wrote:
> Dear All,
> Thank you for the information. I think I've some idea what the problem is
> about after seeing the replies.
> More information about my system and my script
> PIII 1Ghz, 512MB RAM, Windows XP SP3
> The script monitors global input using PyHook,
> and calculates on the information collected from the events to output some
> numbers. Based on the numbers, the script then performs some automation
> using SendKeys module.
> here is the memory usage:
> firefox.exe, 69MB, 109MB
> svchost.exe, 26MB, 17MB
> pythonw.exe, 22MB, 17MB
> searchindexer.exe, 16MB, 19MB
> My first guess is that the script calculated for too long time after
> receiving an event before propagating it to the default handler, resulting
> the system to be non-responsive. I will try to implement the calculation
> part in another thread.
> Then the separate will have 100% CPU usage, hope the task scheduling of
> Windows works in my favour.
(You top-posted this message, putting the whole stream out of order.  So 
I deleted the history.)

All my assumptions about your environment are now invalid.  You don't 
have a CPU-bound application, you have a Windows application with event 
loop.  Further, you're using SendKeys to generate a keystroke to the 
other process.  So there are many things that could be affecting your 
latency, and all my previous guesses are useless.

Adding threads to your application will probably slow the system down 
much more.  You need to find out what your present problem is before 
complicating it.

You haven't really described the problem.  You say the system is 
unresponsive, but you made it that way by creating a global hook;  a 
notoriously inefficient mechanism.  That global hook inserts code into 
every process in the system, and you've got a pretty low-end environment 
to begin with.  So what's the real problem, and how severe is it?  And 
how will you measure improvement?  The Task manager numbers are probably 

My first question is whether the pyHook event is calling the SendKeys 
function directly (after your "lengthy" calculation) or whether there 
are other events firing off  in between.  If it's all being done in the 
one event, then measure its time, and gather some statistics (min time, 
max time, average...).  The task manager has far too simplistic 
visibility to be useful for this purpose.

What else is this application doing when it's waiting for a pyHook 
call?  Whose event loop implementation are you using?  And the program 
you're trying to control -- is there perhaps another way in?


More information about the Python-list mailing list