2012-08-09 127 views
3

我寫了(主要是複製)一個非常簡單的python腳本,作爲xbmc的Caller ID插件。除了關閉套接字外,它可以按預期工作。我已經確認xbmc.abortRequested(XBMC正在關閉的通知)確實被設置爲True,因此循環應該結束。但它沒有(它似乎掛起),xbmc在清理過程中約5秒後殺死腳本。這種不合理的退出並不會引起任何問題,但我寧願讓腳本正常退出。我無法弄清楚是否有某種其他的超時可以設置或什麼。謝謝你的幫助。asyncore套接字沒有正確關閉

道格

我的代碼:

import socket, threading, thread, sys, asyncore, xbmc, xbmcgui, xbmcaddon 
from time import * 
from string import * 

xbmc.log("YAC Listener: Starting") 
PORT = 10629 

class Server(asyncore.dispatcher): 
    def __init__(self, host, port): 
     asyncore.dispatcher.__init__(self) 
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM) 
     self.bind((host, port)) 
     self.listen(1) 

    def handle_accept(self): 
     socket, address = self.accept() 
     ConnectionHandler(socket) 

    def handle_close(self): 
     self.close() 
     xbmc.log("YAC Listener: Closing Port") 

class ConnectionHandler(asyncore.dispatcher_with_send): 
    def handle_read(self): 
     self.buffer = self.recv(1024) 
     self.buffer = split(self.buffer[5:], "~") 
     self.close() 
     global data 
     if len(self.buffer) > 1: 
      name = self.buffer[0] 
      number = self.buffer[1] 
       xbmc.executebuiltin("XBMC.Notification("+name+","+number+",7000,special://home/addons/script.yaclistener/phone.png)") 
     else: 
      data = self.buffer 

s = Server('', PORT) 

while not xbmc.abortRequested: 
    asyncore.loop(timeout=1) 

s.close() 
sys.exit() 
xbmc.log("YAC Listener: Exiting") 

回答

1

「超時」 具有稍微不同的含義(大致可被視爲一個環粒度)。

所以最終文件的代碼應該像水木清華象下面這樣:

... 
while not xbmc.abortRequested: 
    asyncore.loop(timeout=1, count=1) 

# this will try to close ALL current connections: 
asyncore.close_all() 
# this will give some time (up to 5 seconds) for things to settle down: 
asyncore.loop(timeout=1, count=5) 
sys.exit() 
xbmc.log("YAC Listener: Exiting")