Will a thread continue to run during stream playback?

  Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
CaffeinatedMike Offline
Junior Member
Posts: 37
Joined: May 2017
Reputation: 0
Post: #1
I'm looking to implement threading in my video add-on and I was wondering if I create a thread will it continue to run during video playback or will it be paused until playback stops? The reason I ask is because I'm trying to figure out if I need to create a service or if I can avoid it.

Quote:pro·gram·mer (n): An organism capable of converting caffeine into code.
find quote
ironic_monkey Offline
Posting Freak
Posts: 1,423
Joined: Nov 2013
Reputation: 66
Post: #2
if you plugin spawns threads you are breaking API and all bets are off. mostly likely it will crash and burn at a later point.
find quote
CaffeinatedMike Offline
Junior Member
Posts: 37
Joined: May 2017
Reputation: 0
Post: #3
(2017-06-19 13:50)ironic_monkey Wrote:  if you plugin spawns threads you are breaking API and all bets are off. mostly likely it will crash and burn at a later point.

I see many plugins implement threading though. Perhaps if I explain why I need the single extra thread. I need to continually poll a link to get the valid cookies to keep an m3u8 alive.

Quote:pro·gram·mer (n): An organism capable of converting caffeine into code.
(This post was last modified: 2017-06-19 13:54 by CaffeinatedMike.)
find quote
Roman_V_M Online
Team-Kodi Member
Posts: 1,117
Joined: Jun 2011
Reputation: 55
Location: Kyiv, Ukraine
Post: #4
Because of an issue with Python threading implementation in Kodi (I'm calling this "issue" because I don't think it is a bug but rather an implementation problem) a child thread always outlive the master Python process, unless explicitly terminated.
So yes, technically you can run background threads in your addon, but you need to make sure that your threads are properly terminated, especially when Kodi sets abortRequested flag.

Raspberry PI 2 + LibreELEC 8 (Kodi 17)
Samsung Galaxy Tab A 10.1 + Kodi 17 for Android
find quote
CaffeinatedMike Offline
Junior Member
Posts: 37
Joined: May 2017
Reputation: 0
Post: #5
(2017-06-19 14:10)Roman_V_M Wrote:  Because of an issue with Python threading implementation in Kodi (I'm calling this "issue" because I don't think it is a bug but rather an implementation problem) a child thread always outlive the master Python process, unless explicitly terminated.
So yes, technically you can run background threads in your addon, but you need to make sure that your threads are properly terminated, especially when Kodi sets abortRequested flag.

Thanks for that information Roman, I hadn't known that children would outlive the master. However, I am making a point to ensure all threads are dead before the addon exits (or at least I think I am). For this particular situation I only create a single thread inside a service like so

Code:
if __name__ == "__main__":

    monitor = LiveMonitor()
    poll_thread = threading.Thread(target=monitor)
    poll_thread.start()
    
    while not monitor.abortRequested():
        # Sleep/wait for abort for 10 seconds
        if monitor.waitForAbort(10):
            # Abort was requested while waiting. We should exit
            break
        live = addon.getSetting('live_url')
        if live != '':
            client = liveclient.UHSClient(live, 'channel')
            client.poll()
    
    del LivePlayer
    del LiveMonitor
    poll_thread.join()

Is this correctly implemented?

A little background:
I'm looking to poll the url every 10 seconds to keep the m3u8 alive. the LiveMonitor process (of the xbmc.Monitor class) inits the LivePlayer process (of the xbmc.Player class) in its init definition. The monitor starts polling the url once the live_url setting has been set (is not '') and the LivePlayer simply looks for the onPlaybackStopped action and sets the live_url setting to ''

Quote:pro·gram·mer (n): An organism capable of converting caffeine into code.
find quote
Roman_V_M Online
Team-Kodi Member
Posts: 1,117
Joined: Jun 2011
Reputation: 55
Location: Kyiv, Ukraine
Post: #6
@CaffeinatedMike

Quote:Thanks for that information Roman, I hadn't known that children would outlive the master.

In the standard CPython implementation Thread.daemon property controls if a child thread will outlive its parent. But in Kodi this flag does not work and the thread will continue to run, unless stopped.

Quote:Is this correctly implemented?

No, it is not. The target parameter of Thread constructor expects a callable function object, and a class instance is not a such object (unless a class explicitly implements __call__ method which is not the case here). Please look into some Python threading tutorials available in the Internet.

Raspberry PI 2 + LibreELEC 8 (Kodi 17)
Samsung Galaxy Tab A 10.1 + Kodi 17 for Android
(This post was last modified: 2017-06-19 14:38 by Roman_V_M.)
find quote