非插件上的套接字操作

时间:2012-04-11 14:48:12

标签: python windows winsock

我有以下课程:

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

我怎样才能避免这种行为,

谢谢,

百里

1 个答案:

答案 0 :(得分: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

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