473,394 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Python sleep doesn't work right in a loop?

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?

Jul 18 '05 #1
11 19263
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?

Jul 18 '05 #2
ri********@yahoo.com 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
Jul 18 '05 #3
ri********@yahoo.com 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.

Jul 18 '05 #4
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.

Jul 18 '05 #5
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.8)
self.textField.SetBackgroundColour(255, 255, 223)
time.sleep(0.8)

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.

Jul 18 '05 #6
ri********@yahoo.com 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.8)
self.textField.SetBackgroundColour(255, 255, 223) self.textField.Update() time.sleep(0.8)

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

Jul 18 '05 #7
ri********@yahoo.com 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.8)
self.textField.SetBackgroundColour(255, 255, 223)
time.sleep(0.8)

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/

Jul 18 '05 #8
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.8)
self.textField.SetBackgroundColour(255, 255, 223)
time.sleep(0.8)

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
Jul 18 '05 #9
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.

Jul 18 '05 #10
ri********@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...ord-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
Jul 18 '05 #11
On Wed, 06 Apr 2005 12:49:51 -0700, ritterhaus 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.8)
self.textField.SetBackgroundColour(255, 255, 223) time.sleep(0.8)

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.


GUIs don't like "time.sleep". All of them come with some sort of "fire a
timing event in X milliseconds and call this handler". Use that instead.

I believe wx's is the wxTimer class, and the wxFutureCall class looks
promising.

If you want to maintain the same basic calling structure, start playing
games with generators; you can write the same function with "yield", and
then call .next() on an iterator every time the timeout triggers. Best of
both worlds.

Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
1
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
0
by: Yue Fei | last post by:
I have a multi thread python code, threads can start immediately if I run on command line, but I can get them started right the way if I call the same code from C/C++. test code like this: from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.