← Back to team overview

sikuli-driver team mailing list archive

[Question #679532]: How to stop waiting in main script by observer handler

 

New question #679532 on Sikuli:
https://answers.launchpad.net/sikuli/+question/679532

Hello! Thx for a good tool.

I have a standard script with loop and try/except logic like:
***
While True:
try:
wait("1.png",300) #up to 5 min waiting
click("1.png")
except FindFailed:
print("cant find image")
***
and it works perfectly.

But sometimes i have an error messeges in my program and i want to handle them whithout many "if - else" constractions and whithout waiting 5 minutes.
I found "onAppear" and "observe" functions and it seems exactly what i need, but i cant stop waiting in main script by handler. For example:

***
def ErrorHandler(event):
print("Error found")

While True:
try:
onAppear("error.png",ErrorHandler)
observe(300,background = True) #set observer
wait("1.png",300) #up to 5 min waiting
click("1.png")
except FindFailed:
print("cant find image")
***

If i get error messege(error.png) after 30 sec - observer catch it, print "Error found", but main script still running and i have to loose 250sec more for waiting 1.png.
Is it possible to force next loop in main script from handler?

I checked two ways:
1) add something like "break" or "continue" in handler function (ok, that was stupid i suppose, it doesnt work even in clear python)
2) raise Exception in handler function to force end of loop in main script.
raise
raise FindFailed
raise FindFailed ("test")
return raise
return FindFailed
and it doesnt work.

Am i doing something wrong or it's not possible at all?

P.S. i found workaround with many loops and with "if exists" instead of long-wait, but it's not beauty and not usable in some more difficult situations (like one possible error in many steps).
for i in range (30):
if exists(1.png)
else if exists(2.png)
wait(3)


-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.