Sleep timer but still responsive?

Dave Angel davea at
Sat Jan 30 11:07:17 CET 2010

JohnnyFive wrote:
> On Jan 29, 9:33 am, Andreas Tawn <andreas.t... at> wrote:
>>> On Jan 28, 4:55 pm, "Gabriel Genellina" <gagsl-... at>
>>> wrote:
>>>> 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?
>>>> --
>>>> Gabriel Genellina
>>> 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.
>> How about this? Responds to ctrl+c, but still sleeps.
>> import time
>> def responsiveSleep(n):
>>     while n > 0:
>>         time.sleep(1)
>>         n -=
>> 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.
You responded to my message off-list, so I have to paste it here.


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

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

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 "going to sleep"
print "Use Ctrl-C to get my attention, and end the program",
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

More information about the Python-list mailing list