views:

223

answers:

2

I defined an handler for EVT_IDLE that does a certain background task for me. (That task is to take completed work from a few processes and integrate it into some object, making a visible change in the GUI.)

The problem is that when the user is not moving the mouse or doing anything, EVT_IDLE doesn't get called more than once. I would like this handler to be working all the time. So I tried calling event.RequestMore() at the end of the handler. Works, but now it takes a whole lot of CPU. (I'm guessing it's just looping excessively on that task.)

I'm willing to limit the number of times the task will be carried out per second; How do I do that?

Or do you have another solution in mind?

A: 

Something like this (executes at most every second):

...

def On_Idle(self, event):
    if not self.queued_batch:
        wx.CallLater(1000, self.Do_Batch)
        self.queued_batch = True

def Do_Batch(self):
    # <- insert your stuff here
    self.queued_batch = False

...

Oh, and don't forget to set self.queued_batch to False in the constructor and maybe call event.RequestMore() in some way in On_Idle.

Toni Ruža
A: 

This sounds like a use case for wxTimerEvent instead of wxIdleEvent. When there is processing to do call wxTimerEvent.Start(). When there isn't any to do, call wxTimerEvent.Stop() and call your methods to do processing from EVT_TIMER.

(note: i use from wxWidghets for C++ and am not familiar with wxPython but I assume they have a similar API)

Grant Limberg