我正在为xbmc开发python服务,我无可救药地陷入困境。 XBMC具有通过JSON-RPC进行通信的TCP API。 XBMC具有服务器TCP套接字,主要用于接收命令和响应,但如果系统中发生某些事情,它会向TCP发送“通知”。问题是我需要创建行为类似于服务器的TCP客户端,因此它能够接收到这个“通知”。无论我在哪里运行socket.recv(4096)
,它都会等待数据并卡住我的代码,因为我需要循环我的代码。代码结构基本上是这样的:
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...')
我尝试了一切线程,多处理,阻塞,非阻塞,没有任何帮助。 谢谢,
答案 0 :(得分:1)
你可能想要select(),poll()或epoll(): http://docs.python.org/library/select.html
这个Python管道进度表应用程序使用select作为示例: http://stromberg.dnsalias.org/~strombrg/reblock.html
如果您知道哪种分隔符将协议的各个部分分开,您可能会发现这很有用,而不需要选择或类似: http://stromberg.dnsalias.org/~strombrg/bufsock.html 它优雅地处理“读取下一个空字节”,“读取最多100个字节”等。