Python in Kodi does not support daemon threads

A quote from the official documentation:

Quote:daemon

A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

The entire Python program exits when no alive non-daemon threads are left.

However it does not work in Kodi Python. Let’s take the following script:

Code:
from __future__ import print_function
from threading import Thread
from time import sleep

def worker():
    i = 1
    while True:
        print('Working {}.....'.format(i))
        i += 1
        sleep(1)

t = Thread(target=worker)
t.daemon = True
t.start()
sleep(5)

In desktop Python (2 or 3) the script will print “Working n…” 5 times and exit. But in Kodi the script will run forever and you will have to kill a Kodi process to terminate it. So obviously the daemon property is not respected.

I noticed this a while ago but forgot about it, and now I’m developing a remote Python debugger with a web-UI which runs in a separate thread, and this came as an unpleasant surprise. In Kodi I have had to add some additional code to guarantee that the web-UI will be terminated when a script being debugged is finished.