Python:选择或轮询穿线.均匀

2024-04-19 16:35:50 发布

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

我最近读到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)], [], [])


Tags: 文件to用户self类型requestlinux事件
1条回答
网友
1楼 · 发布于 2024-04-19 16:35:50

Is there some clever way to to check a threading.Event() with select or poll or is it an inevitable situation that one has to connect a second socket to listen for a shutdown event?

我不确定我是否正确理解您的意思,但是如果您想检测到对等机的关闭,您可以简单地使用select和poll,然后进行read。也就是说,如果select/poll返回为读取而读取的套接字,然后读取并返回没有数据但也没有错误,那么您就知道对等方发出了关闭。在

不需要第二个套接字,当然第二个连接甚至无法检测第一个连接是否已关闭。在

相关问题 更多 >