How would I stop a while loop after n amount of time?

Asked by Winnie Hsu

how would I stop a while loop after 20 seconds if it does not achieve what I want it to achieve.

like this:

from interruptingcow import timeout
try:
    with timeout(60*5, exception=RuntimeError):
        while True:
except RuntimeError:
    pass

------------------------------------------------------------
My Code is as Follows:
while not exists(A.png): #A.png=9999
    mouseMove(B.png) #B.png=PLUS
    mouseDown(Button.LEFT)
else:
    print("check!")
mouseUp()

p.s. I want to add from 0 until 9999, and don't want use click instead mouseDown,
hope someone can help me, thanks.

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
Best RaiMan (raimund-hocke) said :
#1

something like that:

stop = time.time() + 20
reason = ""

while not exists(A.png): #A.png=9999
    mouseMove(B.png) #B.png=PLUS
    mouseDown(Button.LEFT)
    if time.time() > stop:
      reason = "timeout"
      break
else:
    reason = "check"
mouseUp()
if not reason == "":
  print "reason =", reason
  # do some error handling

Revision history for this message
Winnie Hsu (winniehsu1993) said :
#2

Thanks RaiMan, that solved my question.