[Tutor] while loops causing python.exe to crash on windows

Walter Prins wprins at gmail.com
Mon Jun 7 12:01:34 CEST 2010


On 7 June 2010 02:37, Alex Hall <mehgcap at gmail.com> wrote:

> I am not sure how else to explain it. I want to loop until the value
> of a variable changes, but while that loop is taking place, the user
> should be able to perform actions set up in a wx.AcceleratorTable.
> Looping, though, causes Windows to tell me that python.exe is not
> responding, so I have to close the entire thing. I guess I am looking
> for a "listener", which will sit in the background and only perform an
> action when it detects a certain thing. In this case, a listener to
> watch for a variable to turn from False to True, then to act when it
> sees that change.
>

Notwithstanding what everyone else have said I'll add the following for what
it's worth:

You need to do some research into how GUI systems typically work,
particularly the message queue and each applications "magic" and hidden
message processing loop etc.  Basically in a GUI app there's a hidden loop
that continually checks for messages (calls) to your application and
dispatches those calls to the handlers defined in your application.
However, if your handlers do not complete quickly, then while they run,
control obviously does not return to this loop again, and consequently no
further events can/will be processed by your app.  Some OS's flag up
applications which do not process their event queues for a while as "not
responding" or similar.

However, in most windowing systems there's a way to process pending messages
that have built up in the queue without actually returning from the handler
that you're in.  In Delphi the call is "Application.ProcessMessages", in VB
it's "DoEvents".  In WxPython it's Yield().  (You can google each of those
to see the same issue in different languages/platforms if you like, it might
be instructive.)

So bottom line, in theory you can call Yield() in your loop and you should
be OK -- your app should suddenly be responsive again even while in the
loop.   This type of solution is however the more "cheap and dirty"
alternative and can introduce it's own set of problems and downsides.  (What
if for example another call/message is processed to the same event handler
from which you called Yield() alrady?  The point is you may open yourself up
to re-entrancy issues if you're not careful.  Also if the loop is very tight
you might end-up consuming a lot of CPU doing nothing, so a sleep() type
call might also be advisable in the loop to prevent spending more time
calling Yield() than doing anything else. )

Anyway, I've done a quick google and the following page seems to be a good
discussion of the above thoughts:
http://wiki.wxpython.org/LongRunningTasks

HTH,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100607/93f5fe60/attachment.html>


More information about the Tutor mailing list