How to set observe() to run in background forever?

Asked by Shekhar

Hi,

I've been trying to write to write a code to observe a region in background forever and act when an image appears.

R = selectRegion()
def axn(event):
 R.highlight(3)
R.onAppear(img1,axn)
R.observe(FOREVER,Backgroud=True)

When i try R.observe(FOREVER,Backgroud=True), i get an error message that observe() takes 1 argument, not 2.

I checked out a similar question https://answers.launchpad.net/sikuli/+question/271996 and tried using R.observeInBackground as per the answer to that question.
A plain R.observeInBackground() without any time specified returns the error :- [error] TypeError ( observeInBackground(): expected 1 args; got 0 ).
R.observeInBackground(FOREVER) results in well, basically nothing - the script just doesn't seem to be running at all (sikuli IDE is visible and editable and the run button is active and clickable). The event handler didn't get called even when the image in question eventually appeared.

Am i doing something wrong?

Thanks in advance

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Manfred Hampl
Solved:
Last query:
Last reply:
Revision history for this message
Shekhar (shekharpr10015) said :
#1

The same thing - basically, nothing - happens even when i try observeInBackground(50) with a number specifying the seconds for which to observe.

Revision history for this message
masuo (masuo-ohara) said :
#2

My environment: (SikuliX1.1.1 / windows7 / Java 1.8.0)
Try this codes.
#----------------
R = selectRegion()
def axn(event):
    event.getRegion().highlight(3) #<--changed

R.onAppear(img1,axn)
R.observeInBackground(FOREVER) #<--changed
#----------------

Revision history for this message
Shekhar (shekharpr10015) said :
#3

The problem still continues - when i run the script with the code suggested by you, still nothing happens. Script just doesn't run but no error messages and nothing in the log either.

My environment: Sikulix 1.1.1, Windows 10, Java 1.8.0

Revision history for this message
masuo (masuo-ohara) said :
#4

Sorry, I added one line.

R = selectRegion()
def axn(event):
    event.getRegion().highlight(3)

R.onAppear(img1,axn)
R.observeInBackground(FOREVER)
popup("Click [OK] to stop") #<---Added

Revision history for this message
Shekhar (shekharpr10015) said :
#5

Hi,

I am sorry, i don't understand what this popup is supposed to do. I am afraid the script still isn't doing anything (except now creating a popup that goes away with a click)

Thanks in advance

Revision history for this message
Best Manfred Hampl (m-hampl) said :
#6

see http://sikulix-2014.readthedocs.io/en/latest/region.html#Region.observeInBackground

"The observation is run in the background, meaning that the observation will be run in a subthread and processing of your script is continued immediately.
Take care, that your script continues with some time consuming stuff."

You need something what the script is doing in the foreground while the observeinbackground is waiting for the image to appear. Otherwise the script immediately continues, and terminates because there is nothing to do.

Actions to keep the foreground "busy" are e.g. sleep(seconds) or wait(,seconds) or a pop-up window as suggested by masuo, or otzher actions like searching and/or clicking on images etc. etc.

If you do not have anything to do in foreground, but just want to wait for the image to appear, you probably better use wait(img1,FOREVER) instead of observerinbackground().

Revision history for this message
Shekhar (shekharpr10015) said :
#7

Thanks Manfred Hampl, that solved my question.