Loop for a fixed time

Asked by Jeff Sant

I want the loop to be executed for 1 minute and then it should exit, is this possible?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

start = time.time()
end = start + 60
while True:
    # some code
    if time.time() > end: break
# first statement after loop

Revision history for this message
Best RaiMan (raimund-hocke) said :
#2

The above snippet makes it possible to check something, if the time is over and you might continue the loop with a modified end value.

If you do not need this option, this version is even shorter:

start = time.time()
end = start + 60
while time.time() < end:
    # some code
# first statement after loop

Revision history for this message
Jeff Sant (sant-jeff) said :
#3

Thanks RaiMan, that solved my question.