异步套接字无法正确关闭

2024-10-02 22:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个非常简单的python脚本(大部分是复制的),作为xbmc的Caller ID插件。除了关闭插座外,它的工作正常。我已经证实了xbmc.abortRequested(XBMC正在关闭的通知)确实设置为True,因此循环应该结束。但它没有(似乎挂起),xbmc在清理过程中大约5秒后会杀死脚本。这种不恰当的退出不会引起任何问题,但我希望脚本能够正确退出。我想不出是否有其他的超时可以设置或者什么。谢谢你的帮助。在

道格

我的代码:

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")

Tags: importself脚本logclosedefbuffersocket
1条回答
网友
1楼 · 发布于 2024-10-02 22:25:02

“timeout”的含义稍有不同(大致可以认为是一个循环粒度)。在

因此,结束文件的代码应该像下面这样的smth:

...
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")

相关问题 更多 >