Two `def` functions, doesn't work, only bottom one works, why

Asked by bhavanitech

Hi,

I have two def function is the sikuli editor and both works but only works when there is only one function, i.e. if there are two function definitions, one after the other (as shown in the linked screenshot) then the bottom one works, the top one doesn't work.

If I remove the bottom one, then the top one works and so both are syntactically both are correct. Why is this, help me

See this screenshot:

https://i.imgur.com/m80LZNF.png

------CODE - BEGIN (DOESN'T WORK)--------------

def SetfocusDevToolStylesFilter(event):
    wait(.35)
    click(Pattern("1627068824162.png").targetOffset(-100,0))

Env.addHotkey("g", KeyModifier.ALT+KeyModifier.CTRL, SetfocusDevToolStylesFilter);

#Another hotkey begin;
def SetfocusDevToolStylesComputedFilter(event):
    wait(.35)
    click(Pattern("1627070834253.png").similar(0.92).targetOffset(-50,0))

Env.addHotkey(Key.PAGE_DOWN, KeyModifier.ALT+KeyModifier.CTRL, SetfocusDevToolStylesComputedFilter)

------CODE - END (DOESN'T WORK)- -------------

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

This works for me:

def hk1(evt):
    print "hk1"

def hk2(evt):
    print "hk2"

def hkx(evt):
    print "terminating"
    global running
    running = False

Env.addHotkey("x", KeyModifier.ALT+KeyModifier.CTRL, hkx);

Env.addHotkey("c", KeyModifier.ALT+KeyModifier.CTRL, hk1);

Env.addHotkey("v", KeyModifier.ALT+KeyModifier.CTRL, hk2);

running = True

while running:
    wait(1)

You need something in your script, to keep it active (here the while loop) and a hotkey for script termination (here x).

Do not use exit() in a callback def, since this might stop the IDE (but can be used if the script is run from commandline only).

Revision history for this message
bhavanitech (bhavanitechconsultant) said :
#2

RaiMan,

Thanks, that solved the issue, another issue I face is the script is consuming too much of RAM like anywhere between 500MB - 1GB. Also exported script is as a runnable jar but didn't notice much reduction of RAM usage (may be slightly).

**SPECS**
Win7 (x64),
6 GB RAM,
500 GB HDD, (C:\ - 12 GB free space)
Thinkpad T520 - intel i5 (4th Gen)
JAVA 8 (update 301 , 2021) - updated

**Screenshots**:

[Win Task Manger - ram usage by java.exe - sikuli _ide/ runnable JAR](https://i.imgur.com/2rvmjuw.png)

[script:](https://i.imgur.com/dP845h9.png)

------------------------CODE start:------------------------------

def SetfocusDevToolStylesFilter(event):
    wait(.05)
    click(Pattern("btnstyles.png").similar(0.40).targetOffset(-30,0))
    wait(.05)
    click(Pattern("inputfilter.png").similar(0.50).targetOffset(-100,0))

Env.addHotkey(Key.END, KeyModifier.ALT+KeyModifier.CTRL, SetfocusDevToolStylesFilter)

def SetfocusDevToolComputedStylesFilter(event):
    wait(.15)
    click(Pattern("btncomputed.png").similar(0.52).targetOffset(-30,0))
    wait(.1)

Env.addHotkey(Key.PAGE_DOWN, KeyModifier.ALT+KeyModifier.CTRL, SetfocusDevToolComputedStylesFilter)

def hkx(evt):
    print "terminating"
    global running
    running = False

Env.addHotkey("x", KeyModifier.ALT+KeyModifier.CTRL, hkx);

running = True

while running:
    wait(1)

------------------------CODE END:------------------------------

Also, how to cleanly exit/termiate the runnable jar, when I press the hotkey ctrl+alt+x (def hkx(evt)) to terminate it shows FATAL ERROR
"System.out is broken (console output) . You will not see any message anymore! Save your work and restart the IDE!"

[FATAL error screenshot](https://i.imgur.com/J3F5P3y.png)

Thanks for your help, very much appreciated.

Revision history for this message
bhavanitech (bhavanitechconsultant) said (last edit ):
#3

Thanks RaiMan, that solved my question. But please read my last comment, related to abnormal RAM usage.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

This is not abnormal. Java has a dynamic memory management. SikuliX usage usually starts at about 300 MB. on longer runs it increases towards 700 - 900 MB. This is due to the standard memory parameters of Java.

If you think, this really has an impact on your system behavior, then you have to dive into the memory management parameters of Java (-X parameters).

I stopped looking at this years ago, since it is rather complicated with little overall effect.
I never really had problems with that even on machines with 4GB physical memory, which these days is the minimum recommended.

Revision history for this message
bhavanitech (bhavanitechconsultant) said (last edit ):
#5

> I stopped looking at this years ago, since it is rather complicated with little overall effect.

Thanks for this quote, saved my many hours, possible days of over-researching just to satiate my perfectionism disorder (Yes, perfectionism is a dis-order, struggling with it for decades, lol).

> I never really had problems with that even on machines with 4GB physical memory

Yes, my system works jaggedly/slowly when I have it running along with Chrome, chrome dev_tools, VSCode etc. I think this was due to the fact that older apps were written in real high level performant programming languages like C, C++ or .net with good memory and GC managment. In contrast nowadays, most desktop apps are javascript based, or optimized for JS runtime, Chrome (think multiple chrome extension, needing their own separate ram allocation) VSCode, Notion (Electron, js based), which isn't a real high level lang, essentially its a scripting interpreter written in C++, an extra steps in talking to hardware. so modern apps will never be as performant as older apps, still this is just my opinion. It may be due to many other factors that you never had problem with 4GB RAM.

I think I wouldn't be using Sikuli anymore. Looking over to AutoIT image search/recognition. Anyways, thank you very much for your help and appreciate your work in keeping this project alive over the years. Good day and Have a nice one. :) :)