我最近读到BSD kqueue
可以接受所有类型的事件,而不仅仅是文件描述符。但是对于Linux用户来说,它看起来像这样:
(来自socketserver python stdlib)
#self being passed to select is a listening socket
try:
while not self.__shutdown_request:
# XXX: Consider using another file descriptor or
# connecting to the socket to wake this up instead of
# polling. Polling reduces our responsiveness to a
# shutdown request and wastes cpu at all other times.
r, w, e = _eintr_retry(select.select, [self], [], [],
poll_interval)
if self in r:
self._handle_request_noblock()
self.service_actions()
有没有什么聪明的方法可以用select
或{threading.Event()
,或者是不可避免地需要连接第二个套接字来监听关闭事件?在
编辑:我要找的是这样的东西:
select.select([self, clever_wrapper(self.__shutdown_request)], [], [])
我不确定我是否正确理解您的意思,但是如果您想检测到对等机的关闭,您可以简单地使用select和poll,然后进行read。也就是说,如果select/poll返回为读取而读取的套接字,然后读取并返回没有数据但也没有错误,那么您就知道对等方发出了关闭。在
不需要第二个套接字,当然第二个连接甚至无法检测第一个连接是否已关闭。在
相关问题 更多 >
编程相关推荐