469,923 Members | 1,382 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Time delay loop - better way?

Greetings,

Q: Is there some way to gracefully suspend a python app for a second or
so then resume? I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong. By the way, I'm also using wxPython, if that helps any.

I need to delay a python program for about a second. Googling around, it
looks like most of the timer stuff has to do with thread handling, but my
application is not threaded, just a small app that has problems printing
to a serial label printer if there's not some delay between labels. The
best solution would be if the freakin printer worked right, but I've tried
all the possible flow control settings in pyserial and the windows
com port, and apparently there just has to be some magic undocumented
delay between labels, or they get screwed up.

Not a huge crisis, I admit, as I can work around with a dumb loop, but
again - I know there's a more correct way to do this.

Thanks,

Conrad
Jul 18 '05 #1
12 18415

For short time periods, you could use

import time
....
time.sleep(1.0) # seconds

wxPython may also provide a sleep function.
/Jean Brouwers
ProphICy Semiconductor, Inc.

In article <pa****************************@nospam.com>, Conrad
<bo***********@nospam.com> wrote:
Greetings,

Q: Is there some way to gracefully suspend a python app for a second or
so then resume? I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong. By the way, I'm also using wxPython, if that helps any.

I need to delay a python program for about a second. Googling around, it
looks like most of the timer stuff has to do with thread handling, but my
application is not threaded, just a small app that has problems printing
to a serial label printer if there's not some delay between labels. The
best solution would be if the freakin printer worked right, but I've tried
all the possible flow control settings in pyserial and the windows
com port, and apparently there just has to be some magic undocumented
delay between labels, or they get screwed up.

Not a huge crisis, I admit, as I can work around with a dumb loop, but
again - I know there's a more correct way to do this.

Thanks,

Conrad

Jul 18 '05 #2

On Thu, 01 Jul 2004 00:19:10 +0000, Jean Brouwers wrote:

For short time periods, you could use

import time
...
time.sleep(1.0) # seconds

wxPython may also provide a sleep function.
/Jean Brouwers
ProphICy Semiconductor, Inc.


Thank you Jean,

There's no defense here - I've used the time module a couple
of times, but had some sort of mental block that had me
thinking it just provided basic date services. duh. Sigh.
I've got to set aside some time learn the basic libraries -
work keeps intruding. That and a pesky need to sleep a few
minutes every 24 hours. At least now my application can
get a little sleep, thanks to you.

Thanks again,

Conrad
Jul 18 '05 #3

"Conrad" <bo***********@nospam.com> wrote in message
news:pa****************************@nospam.com...
Greetings,

Q: Is there some way to gracefully suspend a python app for a second or
so then resume?
time.sleep(secs)
I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong.


I believe such loops either hog the CPU or take longer than intended.

Terry


Jul 18 '05 #4
"Terry Reedy" <tj*****@udel.edu> wrote in message news:<ma**************************************@pyt hon.org>...
"Conrad" <bo***********@nospam.com> wrote in message
news:pa****************************@nospam.com...
Greetings,

Q: Is there some way to gracefully suspend a python app for a second or
so then resume?


time.sleep(secs)
I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong.


I believe such loops either hog the CPU or take longer than intended.


Or even worse, a faster CPU comes out and the loop is *shorter* than intended.
Jul 18 '05 #5
Dan Bishop wrote:
"Terry Reedy" <tj*****@udel.edu> wrote in message
news:<ma**************************************@pyt hon.org>...
"Conrad" <bo***********@nospam.com> wrote in message
news:pa****************************@nospam.com...
> Greetings,
>
> Q: Is there some way to gracefully suspend a python app for a second or
> so then resume?


time.sleep(secs)
>I could write the classic basic dumb
> loop-tenzillion-times delay, but that seems inelegant, and well,
> just wrong.


I believe such loops either hog the CPU or take longer than intended.


Or even worse, a faster CPU comes out and the loop is *shorter* than
intended.


Well, you could adjust the delay to CPU speed. But avoid 16-bit counters if
you expect your program to run on fast machines with more than 20 MHz :-)
http://www.merlyn.demon.co.uk/pas-r200.htm#R200

Peter

Jul 18 '05 #6

"Conrad" <bo***********@nospam.com> wrote in message
news:pa****************************@nospam.com...
Greetings,

Q: Is there some way to gracefully suspend a python app for a second or
so then resume? I could write the classic basic dumb


time.sleep()
Jul 18 '05 #7
> Q: Is there some way to gracefully suspend a python app for a second or
so then resume? I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong. By the way, I'm also using wxPython, if that helps any.


wxTimer

The wxPython demo and documentation are your friends.

- Josiah
Jul 18 '05 #8
On Thu, 01 Jul 2004 23:28:17 -0700, Josiah Carlson <jc******@uci.edu> wrote:
Q: Is there some way to gracefully suspend a python app for a second or
so then resume? I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong. By the way, I'm also using wxPython, if that helps any.


wxTimer

The wxPython demo and documentation are your friends.


Note, though, that wxTimer guarantees only 1Hz-1000Hz. On windows, you
get only about 40-50Hz. This makes it completely unsuitable for some tasks.

Jul 18 '05 #9

On Sat, 3 Jul 2004 13:16:59 +1000
Anthony Baxter <an***********@gmail.com> wrote:
On Thu, 01 Jul 2004 23:28:17 -0700, Josiah Carlson <jc******@uci.edu> wrote:
Q: Is there some way to gracefully suspend a python app for a second or
so then resume? I could write the classic basic dumb
loop-tenzillion-times delay, but that seems inelegant, and well, just
wrong. By the way, I'm also using wxPython, if that helps any.


wxTimer

The wxPython demo and documentation are your friends.


Note, though, that wxTimer guarantees only 1Hz-1000Hz. On windows, you
get only about 40-50Hz. This makes it completely unsuitable for some tasks.


I have used wxTimers for doing asyncore.poll() scheduling every 1/100th
a second, and it seemed to work pretty reliably on my celeron 400.
- Josiah
Jul 18 '05 #10
On Sat, 03 Jul 2004 00:50:40 -0700, Josiah Carlson <jc******@uci.edu> wrote:
I have used wxTimers for doing asyncore.poll() scheduling every 1/100th
a second, and it seemed to work pretty reliably on my celeron 400.


On Windows, shtoom was completely unable to use the wxTimer approach -
it's timer precision is just horrific. We ended up splitting the UI
off into a separate thread than all the time-senstive network stuff. I
will admit that shtoom's requirements are a hell of a lot harsher than
your "typical" application's, though.

(twisted's wxreactor uses wxTimer)

Anthony

Jul 18 '05 #11
Anthony Baxter wrote:
On Sat, 03 Jul 2004 00:50:40 -0700, Josiah Carlson <jc******@uci.edu> wrote:
I have used wxTimers for doing asyncore.poll() scheduling every 1/100th
a second, and it seemed to work pretty reliably on my celeron 400.

On Windows, shtoom was completely unable to use the wxTimer approach -
it's timer precision is just horrific. We ended up splitting the UI
off into a separate thread than all the time-senstive network stuff. I
will admit that shtoom's requirements are a hell of a lot harsher than
your "typical" application's, though.

(twisted's wxreactor uses wxTimer)

Anthony

Great, does shtoom have a wx version now? Was wanting that, I'll check
it out.

IIRC, there are more high-precision timers available on win32 than the
ones used by wxTimer, and if you really wanted you could write a wrapper
for them. I created a wx wrapper for waitable timers on win32 a few
years ago; one could combine this with some other stuff to make them
more high precision ; see
http://www.codeguru.com/Cpp/W-P/syst...cle.php/c5759/

Just some thoughts
David
Jul 18 '05 #12
On Mon, 05 Jul 2004 12:10:56 +0200, David Fraser <da****@sjsoft.com> wrote:
Great, does shtoom have a wx version now? Was wanting that, I'll check
it out.
In the svn repository, yes.
IIRC, there are more high-precision timers available on win32 than the
ones used by wxTimer, and if you really wanted you could write a wrapper
for them. I created a wx wrapper for waitable timers on win32 a few
years ago; one could combine this with some other stuff to make them
more high precision ; see
http://www.codeguru.com/Cpp/W-P/syst...cle.php/c5759/


Ta for the pointer. For now, the timer provided by the twisted event
loop on windows is quite good enough, and I get it for free with the
event loop, so I'll probably stick with that. I seem to recall that
there were only a limited number of these high-precision timers on
Windows - in addition, I need to have multiple timer event loops
running in a given process, so many/most pure timer operations aren't
at all suitable :-/

Jul 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by | last post: by
6 posts views Thread by lucifer | last post: by
11 posts views Thread by Maheshkumar.R | last post: by
12 posts views Thread by webinfinite | last post: by
9 posts views Thread by Martin Wells | last post: by
8 posts views Thread by Craig Williamson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.