Python sleep doesn't work right in a loop?

Diez B. Roggisch deetsNOSPAM at web.de
Wed Apr 6 17:32:02 EDT 2005


ritterhaus at yahoo.com wrote:

> Actually, I've tried ALL of these things, and none of them work. I HAVE
> run the simple for-print-sleep test code to try to determine if this
> issue was specific to wx (that's good troubleshooting, folks - you
> narrow down the problem) and even that didn't work, so I thought I'd
> start with the simple problem first. Sorry if you were mislead.

But it _is_ no problem - the code you gave us works for me and others as
expected - on a very similar system, btw. So there _is_ a difference - do
you call the script from an ide, pipe it or something similar?

And as you yourself found that 4 time.sleep(2) produce 8 seconds timeout in
total, it is pretty obvious that the problem is _not_ time.sleep - but that
whatever effect you want to produce between the calls is not happening. For
the print, we gave you some suggestions.
 
> As for wx, I HAVE used the for-setColour-refresh-update-sleep loop,
> still no dice. The last thing the I put in the loop works (again, as if
> all the changes are being queued) but the sleep just 'takes over' the
> loop and nothing happens until al the sleeps are done. With or without
> wx. Not good.

certainly not good - maybe you should read this

http://fraca7.free.fr/blog/index.php?2005/04/04/10-a-word-about-guis

One quote from the comments:

'''
I am working on a major project, using Python+Twisted. I started using
wxPython for the GUI, then discovered that it was completely unuseable. The
wxWidgets event loop is hidden away, and the only way Twisted can integrate
(the reactor) is to set a timer and run for a few cycles every so often.
The resulting app does not paint properly, has huge lags, and is basically
useless. I rewrote it using PyGtk and the GUI is nice and smooth, thanks to
the flexible event loop design. I am not likely to use wxWidgets again,
I've had many stability problems with it under both Linux and Mac, and the
current stable version doesn't even support the latest Gtk.
 So that's my rant too I guess, agree with all you wrote above.
'''

If you can live without windows compatibility - or wait for qt4 - and the
GPL, I suggest a switch to qt.

Sleep does not "takeover" a loop - there is no semantics defined for that.
Try putting the flushes after the prints, and you'll see things work. What
wx influences are on this  I can't say - but the quote above suggests that
event handling is not as straight in wx as it should be.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list