473,698 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19382
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********@yaho o.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.flus h()
time.sleep(1)
print 'Off'
sys.stdout.flus h()
time.sleep(1)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
ri********@yaho o.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. SetBackgroundCo lour(255, 0, 0)
time.sleep(0.8)
self.textField. SetBackgroundCo lour(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********@yaho o.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. SetBackgroundCo lour(255, 0, 0) self.textField. Update() time.sleep(0.8)
self.textField. SetBackgroundCo lour(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********@yaho o.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. SetBackgroundCo lour(255, 0, 0)
time.sleep(0.8)
self.textField. SetBackgroundCo lour(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. SetBackgroundCo lour(255, 0, 0)
time.sleep(0.8)
self.textField. SetBackgroundCo lour(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 SetBackgroundCo lour 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

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

Similar topics

699
33941
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
49
2844
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 Vainio http://www.students.tut.fi/~vainio24
23
2485
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 thread? -- Antoon Pardon
68
5856
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
3
2829
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 > http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body > 'help' to
21
18732
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 nanosleep(), depending on platform }
1
3690
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 # # Brick & Ball in Python # by Jerry Fleming <jerryfleming@etang.com>
7
1978
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 isn't very useful if you want to fetch information from an existing document in the window. One solution I came up with was quite simple; open the "new" window, giving you a reference to the object, then use window.history.back() to
0
1523
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 threading import Thread import thread class testThread(Thread): def __init__ (self, id): Thread.__init__(self) self.id = id def run(self):
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.