Time delay loop - better way?

C

Conrad

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
 
T

Terry Reedy

Conrad said:
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
 
J

Jean Brouwers

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.
 
C

Conrad

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
 
D

Dan Bishop

Terry Reedy said:
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.
 
F

Frithiof Andreas Jensen

Conrad said:
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()
 
J

Josiah Carlson

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
 
A

Anthony Baxter

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.
 
J

Josiah Carlson

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
 
A

Anthony Baxter

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
 
D

David Fraser

Anthony said:
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/system/timers/article.php/c5759/

Just some thoughts
David
 
A

Anthony Baxter

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/system/timers/article.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 :-/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top