0

我有以下课程:

from socket import *
import select

class UdpListenerPort:
    def __init__(self, socket):
        self.socket = socket
        self.socket.setblocking(0)

    def get_data_to_dispatch(self):
        is_ready = select.select([self.socket],[],[],0.01)
        if is_ready[0]:
            return self.socket.recvfrom(0xffff)[0]
        else:
            return None

它从一个线程内运行。当我退出线程时,出现以下异常:

return self.socket.recvfrom(0xffff)[0]
socket.error: [Errno 10038] An operation was attempted on something that is not a socket

我怎样才能避免这种情况,

谢谢,

巴里

4

1 回答 1

0

我发现了问题。我将线程实现为上下文管理器。在退出方法中,我有以下内容:

def __exit__(self, type, value, traceback):
    try:
        self.is_alive = False
        while not self.is_finished:
            time.sleep(0.1)
    except:
        self.is_finished = True

以便优雅地退出线程。但是,我忘了导入时间... :)

于 2012-04-11T15:19:39.353 回答