High idle load under winsdows
Carlos Ribeiro
cribeiro at mail.inet.com.br
Wed Apr 18 08:00:31 EDT 2001
At 10:50 18/04/01 +0000, Mario Premke wrote:
>Hi, I have a python application running under windows which takes
>half of the CPU time when being idle. The idle main-loop does a
>self.ProcessIdle ... Is it possible that this function consumes so
>much CPU power ? There are no other processes in the background
>and the application is definetely doing 'nothing' !!
Idle loops may consume a lot of CPU time, specially if you don't cooperate
with Windows. You have to use some (and preferably many) of the techniques
below to make sure that your program does not burn all available CPU time
running in circles:
a) even if your application is very simple (its not a "true" windows
application), it is a good idea to call pythoncom.PumpWaitingMessages()
inside the busy loop. This call will give back control to Windows, giving
the other applications a chance to run. In some programs of mine, I have
some big files to read and process line-by-line. One time at every 1000
lines I make such a call. The program runs a bit slower, but the overall
behavior of the OS is much better. It also makes it easier to debug,
because it is easier for the debugger to interrupt into the running code
(the debugger needs to get its events also :-)
b) it your program does not use the Windows message queue, AND if you are
explicitly waiting for input (for instance, reading a line character by
character), you should call pythoncom.PumpWaitingMessages() inside the
loop, at *every* iteration.
c) in some cases, your application may "sleep" for a given period of time.
There are some practical ways to make it: using the select call; using a
timer; using the sleep call; etc. (i'm surre you can discover other ways to
do the same). If you are waiting for some data to arrive on a file-like
object, select is a nice way to make it.
If you application is completely event-driven, a combination of the
techniques above will spend *insignificant* time on the main loop.
Carlos Ribeiro
More information about the Python-list
mailing list