Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python sleep doesn't work right in a loop?

Reply
Thread Tools

Python sleep doesn't work right in a loop?

 
 
ritterhaus@yahoo.com
Guest
Posts: n/a
 
      04-06-2005
Just a simple bit of code to toggle between two state at intervals...

import time
for i in range(4):
print 'On'
time.sleep(1)
print 'Off'
time.sleep(1)

.... SHOULD toggle On and Off four times with one-second pauses. When I
run this, the loop pauses the full eight seconds then prints the Ons
and Offs all at once. What's up with that?

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      04-06-2005
For me it works fine. It seems that for you stdout is not flushed
correctly in your terminal. What kind of terminal are you writing to?

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Just a simple bit of code to toggle between two state at intervals...
>
> import time
> for i in range(4):
> print 'On'
> time.sleep(1)
> print 'Off'
> time.sleep(1)
>
> ... SHOULD toggle On and Off four times with one-second pauses. When I
> run this, the loop pauses the full eight seconds then prints the Ons
> and Offs all at once. What's up with that?


Works for me - on a terminal using linux. BUT what not works is this:

python /tmp/test.py | cat

(test.py contains your code of course) The reason is buffered pipes being
used. Try this:

import time, sys
for i in range(4):
print 'On'
sys.stdout.flush()
time.sleep(1)
print 'Off'
sys.stdout.flush()
time.sleep(1)

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
Mike Rovner
Guest
Posts: n/a
 
      04-06-2005
(E-Mail Removed) wrote:
> ... SHOULD toggle On and Off four times with one-second pauses. When I
> run this, the loop pauses the full eight seconds then prints the Ons
> and Offs all at once. What's up with that?


Run your script as:

python -u script.py

for unbuffered output.

 
Reply With Quote
 
ritterhaus@yahoo.com
Guest
Posts: n/a
 
      04-06-2005
This is running in the interactive 'PyShell', but in truth those print
statements are part of a gui app that flashes a control in wx.widgets
by toggling it's background color. The same behavior either way.

 
Reply With Quote
 
ritterhaus@yahoo.com
Guest
Posts: n/a
 
      04-06-2005
Nope. Does't work. Running Python 2.3.4 on Debian, Linux kernel 2.6.
This is actually test code for a larger project...

# flash the selected wx.TextControl

for flasher in range(4):
self.textField.SetBackgroundColour(255, 0, 0)
time.sleep(0.
self.textField.SetBackgroundColour(255, 255, 223)
time.sleep(0.

Even when I add an explicit call to repaint the TextCtrl between each
sleep, things appear to be 'queued' until after the loop is fnished.
Very bizarre.

 
Reply With Quote
 
Mike Rovner
Guest
Posts: n/a
 
      04-06-2005
(E-Mail Removed) wrote:
> Nope. Does't work. Running Python 2.3.4 on Debian, Linux kernel 2.6.
> This is actually test code for a larger project...
>
> # flash the selected wx.TextControl
>
> for flasher in range(4):
> self.textField.SetBackgroundColour(255, 0, 0)

self.textField.Update()
> time.sleep(0.
> self.textField.SetBackgroundColour(255, 255, 223)

self.textField.Update()
> time.sleep(0.
>
> Even when I add an explicit call to repaint the TextCtrl between each
> sleep, things appear to be 'queued' until after the loop is fnished.
> Very bizarre.


If you use .Refresh() request still queued.

/m

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      04-06-2005
(E-Mail Removed) wrote:
> Nope. Does't work. Running Python 2.3.4 on Debian, Linux kernel 2.6.
> This is actually test code for a larger project...
>
> # flash the selected wx.TextControl
>
> for flasher in range(4):
> self.textField.SetBackgroundColour(255, 0, 0)
> time.sleep(0.
> self.textField.SetBackgroundColour(255, 255, 223)
> time.sleep(0.
>
> Even when I add an explicit call to repaint the TextCtrl between each
> sleep, things appear to be 'queued' until after the loop is fnished.
> Very bizarre.
>

Not at all, but completely unrelated to your initial question.

You need to use a specific call before each sleep to tell wxPython to
update the display, since the sleep doesn't give control back to the
display subsystem. I think the call you need is app.Yield(), but the
docs will confirm that.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-06-2005

> Nope. Does't work. Running Python 2.3.4 on Debian, Linux kernel 2.6.
> This is actually test code for a larger project...


No Nope - it _does_ work. Did you actually try it? Because you use it in a
wrong context does not mean that it doesn't work. Besides, giving a wrong
example to prove a point is always a bad idea.

> # flash the selected wx.TextControl
>
> for flasher in range(4):
> self.textField.SetBackgroundColour(255, 0, 0)
> time.sleep(0.
> self.textField.SetBackgroundColour(255, 255, 223)
> time.sleep(0.
>
> Even when I add an explicit call to repaint the TextCtrl between each
> sleep, things appear to be 'queued' until after the loop is fnished.
> Very bizarre.


That's a totally different thing - no idea how wx works in detail, but the
behaviour you describe looks as if the calls to SetBackgroundColour are
queued until the event loop is processed again. So check how to do that
manually between calls, and things are most probably working.

Again - giving the above example would made us give you that advice way
earlier - and saved us digging in the wrong direction...

--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
ritterhaus@yahoo.com
Guest
Posts: n/a
 
      04-06-2005
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.

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sleep 0.2 acts more like sleep 1 Richard Ruby 7 05-22-2007 08:02 PM
Right-Click With Mouse and Toolba Buttonsr Don't Work Right Bigfoot It Is Computer Support 0 10-30-2006 06:08 PM
pulling my hair out, why won't Kernel.sleep(0) sleep? Sam Roberts Ruby 16 02-18-2005 08:27 PM
Thread.sleep dose not work from the first time???? al ASP .Net 3 08-03-2004 02:41 PM
What is the difference between Thread.sleep(10) and Thread.currentThread().sleep(10) ? Gonzalo Moreno Java 2 04-05-2004 05:41 PM



Advertisments