Custom theading best practices

Asked by Hugo Jose Goncalves on 2019-07-23

Are there any recommended approach for custom threading?
I'm already using observers which work pretty well for some cenarios, but I want to leave a thread running some tasks until a observer gets triggered and stops that task.

I'm using something like this ->
`
     myreg.onVanish(stopTask)
     myreg.observeInBackground(FOREVER)

     taskThread = threading.Thread(target = myTask, args = (taskArg, ))
     taskThread.daemon = True
     taskThread.start()

     while True:
            # keep alive stuff
`

I ran into a few things like **taskThread** not getting stopped with **alt+shift+c** for example
I kinda managed to make it work fine using a custom stop HK.
Just wondering if there are any best practices about custom threading and SikuliX as I couldn't find any information.

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2019-08-20
Last reply:
2019-08-20
RaiMan (raimund-hocke) said : #1

latest 1.1.4 should reliably work with alt-shift-c.

Hugo Jose Goncalves (hugo159) said : #2

even with build 364 daemon threads doenst get killed using alt+shift+c, same issue if the sikulix ends due an exception

RaiMan (raimund-hocke) said : #3

ok, I have to check with your special threading usage.

RaiMan (raimund-hocke) said : #4

I tested and found the problem is:
taskThread.daemon = True

This seems to completely detach the thread from the running script's context to the global context of the IDE.
Everything works fine, if you leave this out.

Why do you think you need it in your case?

Hugo Jose Goncalves (hugo159) said : #5

Oh, I don't need at all
I may have misread python docs about threading
Long time I don't use it at all.

Gonna test it tonight, sorry for bothering.

RaiMan (raimund-hocke) said : #6

no problem. Everything is worth to be evaluated and might lead to enhancements ;-)

Hugo Jose Goncalves (hugo159) said : #7

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\hugo1\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib\atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "C:\Users\hugo1\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib\threading.py", line 297, in _MainThread__exitfunc
    t.join()
  File "C:\Users\hugo1\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib\threading.py", line 137, in join
    self._thread.join()
InterruptedException: java.lang.InterruptedException
Error in sys.exitfunc:
Traceback (most recent call last):
  File "C:\Users\hugo1\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib\atexit.py", line 24, in _run_exitfuncs
  File "C:\Users\hugo1\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib\threading.py", line 297, in _MainThread__exitfunc
  File "C:\Users\hugo1\AppData\Roaming\Sikulix\Extensions\jython-standalone-2.7.1.jar\Lib\threading.py", line 137, in join
 at java.lang.Object.wait(Native Method)
 at java.lang.Thread.join(Unknown Source)
 at java.lang.Thread.join(Unknown Source)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.InterruptedException: java.lang.InterruptedException

I don't think it should be waiting (joinning) these custom threads as they have a infinite loop
that's why I thought it made a lot of sense to mark them as daemon

RaiMan (raimund-hocke) said : #8

this is my test on Windows 10/Java 12:

import threading
def myTask():
    n = 0
    while True:
        print n
        n += 1
        wait(1)
taskThread = threading.Thread(target = myTask)
taskThread.start()
while True:
    wait(1)

When running the script from inside IDE:
Using alt-shift-c (abort key) terminates everything and returns to the IDE

When running the script from commandline:
Problem: alt-shift-c (abort key) currently does not work (I have to fix this)
... but ctrl-c allows to terminate the complete run (with an additional confirmation when using a batch script)

Can you help with this problem?

Provide an answer of your own, or ask Hugo Jose Goncalves for more information if necessary.

To post a message you must log in.