Pause main program during event handler Sikuli

365 Views Asked by At

I have an Observer running in background forever, that is looking for a specific pattern, and will call the handler rdp_handler when triggererd:

onAppear("1615387075076.png", rdp_handler)
observeInBackground()

The handler is supposed to wait FOREVER or to wait for the pattern to vanish:

print "rdp_handler called"
waitVanish("1615387075076.png", FOREVER)
print "rdp_handler finished"

Basically what I'm trying to do is pause the execution of the script until the pattern vanish, obviously this doesn't work because the handler is executed in a Thread, so the Thread is paused but my main programm keep running.

I tried to import psutil to suspend the java process, but I get the following traceback (after adding the path to the site-package directory):

[error] script [ OrderCreate ] stopped with error in line 11
[error] NotImplementedError ( platform java13.0.2 is not supported )
[error] --- Traceback --- error source first
line: module ( function ) statement 
143: __init__ (  <module> )     raise NotImplementedError('platform %s is not supported' % sys.platform)
11: main (  <module> )     import psutil
[error] --- Traceback --- end --------------

After some research I found out that I can only import java and pur python lib, psutil has some C code in it...

Here come my question: do you have a clue on how to proceed to pause the main programm until the handler is done doing his stuff ?

3

There are 3 best solutions below

1
Curtis On

Try:

import time
while(not waitVanish("1615387075076.png", FOREVER)):
    time.sleep(1)

This should check every second for vanishing.

0
Ben On

Soooo after trying some funky stuff, I managed to get something working, the only problem is: I have to add some if statement at crutial part of my script:

at the start I define my event and start my observer:

my_event = onAppear("1615387075076.png")
observeInBackground()

I then created a function:

def rdp_handler(my_event):
    if exists("1615387075076.png"):
        print("RDP disconnected")
        waitVanish("1615387075076.png", FOREVER)
        print("RDP reconnected")
        setActive(my_event)

Now each time I interact with the interface, I do this:

if hasEvents():
    SageUtils.rdp_handler(my_event)

hasEvents() return True if my pattern appeared at one time of my program, if it's True, I call rdp_handler(my_event) that will check if the pattern is still on screen, if it is, then it will wait for it to vanish, and reactivate my event used in my observer.

It is ugly but hey, it work.

I'm still opened to better/cleaner solution, but yeah so far I'm happy.

2
RaiMan On

RaiMan from SikuliX:

What you want is currently not available as a feature of SikuliX.

This is the best answer you can get here: https://stackoverflow.com/a/16899837/1918124

... but there is indeed a hidden feature, that at least allows to block the script, the next time it tries to issue a mouse action. I will try with your case the next days, wether this principally works.

If you are interested, add an issue here, so you can get a follow up.