Comment 3 for bug 778925

Revision history for this message
RaiMan (raimund-hocke) wrote :

Thanks again for the challenge ;-)

Of course this is possible in Python/Jython ...
... and here is a rather basic solution

you can:
- add as many images to be searched in parallel
- give each one an individual wait time
- stop all running searches, if you want at any time

import thread

# the threadable find function
def tfind(parms):
    global shouldStop
    end = time.time()+parms.get("waittime", 3) # standard wait time 3 seconds
    reg = parms.get("region", SCREEN) # standard region whole screen
    parms["match"] = None
    parms["finished"] = False
    while (not shouldStop) and time.time()<end:
        if reg.exists(parms["pattern"], 0):
            parms["match"] = reg.getLastMatch()
            break
    parms["finished"] = True

# setup a pattern list to be paralleled
pats = []
# model pattern: {"pattern": some image or pattern, "waittime": seconds, "region": some region}
pats.append({"pattern":"1352549030079.png"})
pats.append({"pattern":"1352551675237.png", "waittime":10})
pats.append({"pattern":"1352551662476.png"})

# start the threads
shouldStop = False
for p in pats:
    thread.start_new_thread(tfind, (p,))

#wait for termination
end = time.time()+5 # search max 5 seconds
while True:
    # stop searches if global search time elapsed
    if time.time() > end:
        shouldStop = True
        print "search stopped"
        wait(2) # give time to the threads to stop
        break
    # wait until all threads have terminated
    ended = True
    for p in pats:
        ended = ended and (p.get("finished", False))
    if ended:
        print "search ended normally"
        break

# check results
for p in pats:
    if p["match"]: p["match"].highlight(1)
    else: print "not found:", p["pattern"]

Happy scripting ;-)

I will implement this in the Sikuli API as something like that:

findAny(pattern, pattern, ..., waittime) # comes back when the first one is found
findAny(listOfPatterns, waittime)

findAll(pattern, pattern, ..., waittime) # comes back after all are found
findAll(listOfPatterns, waittime)

both come back latest after wait time and return a list of matches corresponding positionally to the given patterns