从外套筒螺纹关闭套筒

2024-09-25 08:41:51 发布

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

我在用一个叫Indigo的Mac自动化程序。在我正在使用的插件中,当插件通过调用openSocket(self)启动时,一个套接字被打开。一旦套接字打开,Indigo方法runConcurrentThread(self)就在一个单独的线程中运行,以从客户机接收任何数据。你知道吗

我的问题是,当调用插件关闭或重新加载时,runConcurrentThread(self)创建的线程应该停止,但它超时并强制关闭。必须先关闭套接字,这样它就不会阻止线程关闭,但我不知道怎么做。你知道吗

如有任何建议,我们将不胜感激。你知道吗

# http://wiki.indigodomo.com/doku.php?id=indigo_6_documentation:plugin_guide
# called by startup(self) on starting the plugin
def openSocket(self):

    self.debugLog('openSocket method called')

    for dev in indigo.devices.iter("self"):
        props = dev.pluginProps

    self.PORT = int(dev.pluginProps['rpiSockPort'])  # Arbitrary non-privileged port 8888 is a good one

    if dev.pluginProps['rpiHost'] == "all":
        HOST = ''
    else:
        HOST = dev.pluginProps['rpiHost'] 

    self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    self.debugLog('Socket created')

    try:
        self.s.bind((HOST, self.PORT))
    except socket.error , msg:
        self.debugLog('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
        sys.exit()

    self.debugLog('Socket bind complete')

    self.s.listen(5)

    self.debugLog('Socket now listening')       


#called by Indigo after startup(self)
def runConcurrentThread(self):

    try:

        while True:
            self.errorLog("runConcurrentThread starting")

            #wait to accept a connection - blocking call
            conn, addr = self.s.accept()
            connIp = addr[0]
            connPort = str(addr[1])

            data = conn.recv(1024)

            reply = 'the server received ' + data + '\n'

            if not data: 
                sys.exit()

            conn.sendall(reply)

            # method to update the data in Indigo
            self.updateDeviceStates(self.PORT, connIp, connPort, data)


    except self.StopThread:
        self.debugLog("rpi main thread stopping")

Tags: thedevself插件hostdataportsocket