【发布时间】:2012-07-03 20:36:42
【问题描述】:
我正在为 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...')
我尝试了所有线程、多处理、阻塞、非阻塞,但没有任何帮助。 谢谢,
【问题讨论】:
-
您在代码中的哪个位置尝试了 recv 命令?
-
你看过使用 select 模块吗? (docs.python.org/library/select.html) (doughellmann.com/PyMOTW/select)
-
@PaulSeeb 无论你在哪里运行 recv,后面的一切都不会执行。
-
我需要像 xbmc.Player 这样会自动执行的类,例如onDataRecieved(self):