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

Asked by Winnie Hsu on 2018-09-10

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:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2018-09-10
Last query:
2018-09-10
Last reply:
2018-09-10
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

Winnie Hsu (winniehsu1993) said : #2

Thanks RaiMan, that solved my question.