XBMC Thread For Polling
#1
Hi All,

I have a plugin currently which allows people to view hockey streams. The suppliers have asked to clamp down on account sharing whether I can implement a polling mechanism in the plugin to check if a user is online in multiple locations.

I have built the following class to help with threading (which is how I assume I will manage a polling situation whilst video is playing).

Code:
class KeepAliveWorker(threading.Thread):

    # Unique id for the keep alive worker, used to find it
    # from the thread pool later
    ID = 'MY ID'

    # Creates a new keep alive worker instance
    # which will poll for keep alive updates
    # @param username the username to make calls with
    # @param sleepTime the time to sleep between calls (in seconds)
    def __init__(self, username, sleepTime):
        self.username = username
        self.sleepTime = sleepTime

        # Keep track of how long we have slept
        self.timeSlept = float('inf')

        # A unique id for reference
        self.id = random.randint(0, 1000000)

        # The time to wait between checks (in seconds)
        self.sleepWait = 0.5

        # Call parent constructor and set as a daemon thread
        threading.Thread.__init__(self)
        self.daemon = True
        self.name = KeepAliveWorker.ID

    def run(self):
        while 1 == 1:
            if self.timeSlept >= self.sleepTime:
                print 'KEEPALIVEWORKER[' + str(self.id) + '] starting keep alive'
                # TODO Make request
                self.timeSlept = 0.0
                print 'KEEPALIVEWORKER[' + str(self.id) + '] finished, sleeping...'
            else:
                self.timeSlept = self.timeSlept + self.sleepWait
                time.sleep(self.sleepWait)

I then use the following block of code each time my script runs to determine if I need to recreate the thread.

Code:
# Method to find whether we need to create the keep
# alive worker by checking the thread pool for activity
def shouldCreateKeepAliveWorker():
    for t in threading.enumerate():
        if t.name == KeepAliveWorker.ID:
            if t.isAlive():
                print 'Found keep alive thread, no need to recreate'
                return False
            else:
                print 'Found keep alive thread but its not alive, need to recreate'
                return True

    print 'Keep alive thread not found, need to create'
    return True

Trouble is this always returns true and no threads are found to exist after the first creation, my logs then indicate I spawn several threads throughout the lifetime of my addon.

Code:
16:01:40 T:7784  NOTICE: KEEPALIVEWORKER[666710] starting keep alive
16:01:40 T:7784  NOTICE: KEEPALIVEWORKER[666710] finished, sleeping...
16:01:48 T:9932  NOTICE: KEEPALIVEWORKER[365993] starting keep alive
16:01:48 T:9932  NOTICE: KEEPALIVEWORKER[365993] finished, sleeping...
16:01:56 T:3416  NOTICE: KEEPALIVEWORKER[413208] starting keep alive
16:01:56 T:3416  NOTICE: KEEPALIVEWORKER[413208] finished, sleeping...

So what is the correct way to achieve this? I assume that xbmc sandboxes the python script on each run so that it cannot see any threads outside its scope.

Is there anyway to set a global parameter e.g. LAST_KEEP_ALIVE_TIME ? And if this was older than my set timeout I could recreate my thread? Or is there a better solution? Many thanks for any help on this.[/code]
Reply
#2
AHA! I may have answered my own question, does it seem reasonable to use a window property for this? So that I update the property on each keep alive with the timestamp, I can then check this property rather than the threads for activity, if the last timestamp does not exist or is over 20 seconds then create a new one?

Code:
xbmcgui.Window(10000).setProperty('addonid.timestamp', str(time.time()))

Code:
def shouldCreateKeepAliveWorker():
   timestamp = xbmcgui.Window(10000).getProperty('addonid.timestamp')
   if timestamp == None:
      return True

   timestamp = int(timestamp)
   if timestamp + 20 > time.time():
      return True
   else:
      return False
Reply

Logout Mark Read Team Forum Stats Members Help
XBMC Thread For Polling0