how to interrupt time.sleep ?

B

BOOGIEMAN

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"

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

Peter Hansen

BOOGIEMAN said:
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
 
D

Dan Sommers

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"
If it can't be canceled what's the other solution to
"wait certain time/or press any button to continue" problem ?

Use the select module.

HTH,
Dan
 
T

Thomas Heller

BOOGIEMAN said:
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"

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

On windows at least, pressing ^C interrupts time.sleep().

print "Press ^C if you don't want to wait"
try:
time.sleep(60)
except KeyboardInterrupt:
pass

Thomas
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top