中断选择添加另一个要在Python中观看的套接字

Dan*_*use 2 python tcp winsock

我正在使用Windows XP应用程序中的TCP实现点对点IPC.

我正在使用Python 2.6.6中的selectsocket模块.

我有三个TCP线程,一个通常阻塞的读取线程select(),一个通常在事件上等待的写入线程(事件表明有要写入TCP的东西)和一个接受连接的侦听线程.

如果我启动一个新连接或关闭当前连接,那么我需要中断读取选择并重新启动它,以便它也会监听新接受的套接字.

winsock我可以调用WSACancelBlockingCall哪个将优雅地中断选择.

所以我的问题是:是否有可能以pythonic方式完成所有这些而无需使用poll()

很多thx

--DM

Ada*_*eld 6

您可以尝试将额外的文件描述符添加到您用作信号机制的集合中.然后,您可以向该描述符写入一个导致select退出的虚拟值.例如:

my_pipe = os.pipe()
...
while True:
    ready_fds = select.select(my_read_fds + [my_pipe[0]],
                              my_write_fds, my_except_fds, timeout)
    if my_pipe[0] in ready_fds[0]:
        # Add another fd to my_read_fds, etc.
        os.read(my_pipe[0], 1)
    ...

# To interrupt the current select call and add a new fd, write to the pipe:
os.write(my_pipe[1], 'x')
Run Code Online (Sandbox Code Playgroud)