time.sleep() under Windows

Thomas Heller theller at python.net
Fri Jan 17 04:36:20 EST 2003


Gerhard Häring <gerhard.haering at opus-gmbh.net> writes:

> Chuck <cdreward at riaa.com> wrote:
> > 
> > In Windows 98, if I type:
> > 
> > time.sleep(5)
> > 
> > in the python shell (GUI or console), the shell itself locks up
> > for 5 seconds, and I cannot stop it with a CTRL-C (or rather, it
> > does not respond to the CTRL-C until all 5 seconds have passed).
> 
> That's normal. I believe Ctrl-C only works if anything is currently
> going on with the stdin/out/err files.
> 
> Btw. try Ctrl-Break (on the Pause key) - it works here to interrupt
> time.sleep on Windows 2000.
> 
> > The python shell locks up - I cannot move the window, minimize it, etc.
> 
> Sounds like a Win98 issue.

That may be, but the behaviour has changed in Python 2.3, at least
if you're using Win2k. Now you can interrupt the time.sleep() call
with CTRL-C, and Python can catch a KeyboardError, which CTLR-Creak
still terminates the process.

Mark Hammond implemented this IIRC, it's amazing how complicated this is.

> 
> > The same script, in Linux, runs the way I want it (you can CTRL-C
> > it, the interpreter doesn't lock up, etc). I realize this is just
> > a platform issue...  But is there something else besides
> > time.sleep() that I can use, that is platform independent?
> 
> There are many functions in the Python standard library that are
> platform independent ;-) But for actually sleeping, I think
> time.sleep is the only option. But maybe your app doesn't really
> need to sleep but wait on data to become available? In that case,
> you could use select.select instead.

You cannot interrupt select on Windows with CTRL-C, IIRC.

Thomas




More information about the Python-list mailing list