1

I am developing python service for xbmc and I am hopelessly stuck. XBMC has TCP API that communicates by JSON-RPC. XBMC has server TCP socket that is mainly design to recieve commands and respond, but if something happens in system it sends "Notification" to TCP. The problem is that I need to create TCP client that behaves like server therefore it is able to recieve this "Notification". Wherever I run socket.recv(4096) it waits for data and stucks my code, because I need to loop my code. Structure of code is basically like this:

import xbmc, xbmcgui, xbmcaddon

class XPlayer(xbmc.Player) :   
    def __init__ (self):
        xbmc.Player.__init__(self)
    def onPlayBackStarted(self):
        if xbmc.Player().isPlayingVideo():
            doPlayBackStartedStuff()

player=XPlayer()
doStartupStuff()

while (not xbmc.abortRequested):
    if xbmc.Player().isPlayingVideo():
        doPlayingVideoStuff()
    else:
        doPlayingEverythingElseStuff()

    xbmc.sleep(500)
    # This loop is the most essential part of code

if (xbmc.abortRequested):
    closeEverything()
    xbmc.log('Aborting...')

I tried everything threading, multiprocessing, blocking, non-blocking and nothing helped. Thanks,

Tomas
  • 331
  • 1
  • 3
  • 10

1 Answers1

1

You likely want select(), poll() or epoll(): http://docs.python.org/library/select.html

This Python pipe-progress-meter application uses select, as an example: http://stromberg.dnsalias.org/~strombrg/reblock.html

If you know what sort of delimiters are separating the various portions of the protocol, you may also find this useful, without a need for select or similar: http://stromberg.dnsalias.org/~strombrg/bufsock.html It deals pretty gracefully with "read to the next null byte", "read a maximum of 100 bytes", etc.

user1277476
  • 2,871
  • 12
  • 10