Sleep timer but still responsive?

J

JohnnyFive

I need help with something that is probably fairly simple, but i'm
having a heck of a time getting it work.

Basically, I need my program to sleep for a certain amount of time,
but I don't want the console to become unresponsive while sleeping.

As soon as the time is up, I want the main program to run it's course
again.

I tried using a Timer, threads, etc, but I really can't figure it out.
What am I missing?

I can post what I have, but I don't want to get caught up on how i'm
doing it wrong (as none of it works), but rather the correct way to do
it.

Thanks in advance!
 
G

Gabriel Genellina

I need help with something that is probably fairly simple, but i'm
having a heck of a time getting it work.

Basically, I need my program to sleep for a certain amount of time,
but I don't want the console to become unresponsive while sleeping.

Please provide more details. What do you want your program to do while
sleeping? What kind of actions do you want a response to?
Do you have a GUI? A curses-based interfase?
 
J

JohnnyFive

Please provide more details. What do you want your program to do while  
sleeping? What kind of actions do you want a response to?
Do you have a GUI? A curses-based interfase?

My app is purely console based. I just don't want the console to lock
up (on Windows using time.sleep(x) causes the console to become
unresponsive until the timer is done), and I want people to be able to
CTRL+C to stop the script if need be (which can't be done if it's
unresponsive!).

Thanks.
 
A

Andreas Tawn

Please provide more details. What do you want your program to do while
sleeping? What kind of actions do you want a response to?
Do you have a GUI? A curses-based interfase?

My app is purely console based. I just don't want the console to lock
up (on Windows using time.sleep(x) causes the console to become
unresponsive until the timer is done), and I want people to be able to
CTRL+C to stop the script if need be (which can't be done if it's
unresponsive!).

Thanks.[/QUOTE]

How about this? Responds to ctrl+c, but still sleeps.

import time

def responsiveSleep(n):
while n > 0:
time.sleep(1)
n -= 1

Cheers,

Drea
 
M

MRAB

JohnnyFive said:
My app is purely console based. I just don't want the console to lock
up (on Windows using time.sleep(x) causes the console to become
unresponsive until the timer is done), and I want people to be able to
CTRL+C to stop the script if need be (which can't be done if it's
unresponsive!).

Thanks.

Which version of Python are you using? time.sleep(x) can be interrupted
in Python 2.6.

If the version you're using can't be interrupted then you could use
multiple sleeps:

# Wait for a total of 10 secs.
for i in range(10):
time.sleep(1)
 
G

Gabriel Genellina

JohnnyFive wrote:

Which version of Python are you using? time.sleep(x) can be interrupted
in Python 2.6.

I'm able to be more precise: time.sleep() can be interrupted in any Python
version since 2.3.

To the OP: beware of any unqualified 'except' clauses; a block like this:

try:
...
except:
do_something_or_pass

may "swallow" the KeyboardInterrupt exception (generated by a Ctrl-C
press).
From Python 2.5 and up, the most generic exception clause should read
`except Exception: ...`
In previous versions, you had to explicitely re-raise KeyboardInterrupt
and SystemExit in any catch-all block:

try:
...
except (KeyboardInterrupt, SystemExit):
raise
except:
...
 
J

JohnnyFive

How about this? Responds to ctrl+c, but still sleeps.

import time

def responsiveSleep(n):
    while n > 0:
        time.sleep(1)
        n -= 1

Cheers,

Drea

Thanks for the ideas! Maybe it's just my computer, but using your
solution still causes the prompt to become unresponsive during the
sleeps.

I am using 2.6.4 btw. It's not a major deal though, I just thought
there had to be a way to do this fairly easily.
 
D

Dave Angel

JohnnyFive said:
Thanks for the ideas! Maybe it's just my computer, but using your
solution still causes the prompt to become unresponsive during the
sleeps.

I am using 2.6.4 btw. It's not a major deal though, I just thought
there had to be a way to do this fairly easily.
You responded to my message off-list, so I have to paste it here.

"""<offlist>
Dave,

Thanks for the response.

Here's my scenario. I have a program that checks an ftp site every hour for
updated files, downloads them, and then sleeps again for another hour when
done.

It's 100% console.

When I run the app, no matter how I try, I can't get the window to act
normal during the sleep cycle. I've tried putting the sleep in another
thread and have the main thread.join(), but it all has the same behavior:
the console window behaves like it is crashing.

Using the small sleep() increments also causes the same thing, the becomes
undraggable for periods of time, and again acts like the program has crashed
(even though it's just asleep).

I am running a version of it right now, and i've managed to minimize it, but
it won't maximize so I can ctrl+c it. All it's doing is exactly what Andreas
recommended.

This is not user-friendly behavior!

I've also tried having the main thread sit at a raw_input, and have another
thread have the timer, but there was odd behavior when I wanted the program
to continue what it was doing, even if the user hadn't pressed "enter" to
get passed the command prompt.

</offlist> """


You probably need to tell us your complete environment. I know you're
running 2.6.4, but don't know which version of Windows, so I'll guess
XP. You're running in a cmd.exe console.

There must be something else going on in your system, since a console
does not become unresponsive during a sleep. The python program is
sleeping, but the console is very much alive; it's a separate process.
So dragging, minimizing, restoring and maximizing is unaffected. Try
the following simple script from a cmd console:

import time
print
print
print "going to sleep"
print "Use Ctrl-C to get my attention, and end the program",
time.sleep(30)
print "done"


On XP SP3, running Python 2.6.4, this ignores regular keystrokes, but
responds nicely to control C. And it can be dragged around, resized,
minimized, etc. with no problem.


If you get different behavior, tell us more precisely how your
environment differs from my guesses. Once we've solved Ctrl-C, drag the
console, minimize the console, then maybe you're going to request
"respond to other keystrokes". It can all be done, but only with more
careful wording than "behaves like its crashing."

Regards, DaveA
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top