how to interrupt time.sleep ?

Peter Hansen peter at engcorp.com
Tue Feb 22 17:03:28 EST 2005


BOOGIEMAN wrote:
> I have line  "time.sleep(60)"  in my code
> 
> How do I cancel waiting 60 seconds if I want to continue with program 
> imediately? Like "Press some button if you don't want to wait"

You cannot.

> If it can't be canceled what's the other solution to 
> "wait certain time/or press any button to continue" problem ?

Generally you can just use a much shorter sleep time, and a loop
that checks for the condition which interests you.

while True:
     if userWantsToContinue():
         break
     time.sleep(0.25)	# wait a short time


The implementation of userWantsToContinue() depends of course on
what you want it to do.  If you need help with that, be sure to
indicate what platform you're on so the answers can be meaningful
if the solution (as is likely in this case) is not platform-
independent.

-Peter



More information about the Python-list mailing list