首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python SocketServer

Python SocketServer
EN

Stack Overflow用户
提问于 2010-10-05 19:40:23
回答 2查看 3.8K关注 0票数 6

收到某条退出消息后,如何在SocketServer中调用shutdown()?据我所知,对serve_forever()的调用将阻塞服务器。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2010-10-05 22:37:57

使用源码,卢克!

摘自SocketServer.py:

代码语言:javascript
复制
   def serve_forever(self, poll_interval=0.5):
        """Handle one request at a time until shutdown.

        Polls for shutdown every poll_interval seconds. Ignores
        self.timeout. If you need to do periodic tasks, do them in
        another thread.
        """
        self.__is_shut_down.clear()
        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 = select.select([self], [], [], poll_interval)
                if self in r:
                    self._handle_request_noblock()
        finally:
            self.__shutdown_request = False
            self.__is_shut_down.set()

    def shutdown(self):
        """Stops the serve_forever loop.

        Blocks until the loop has finished. This must be called while
        serve_forever() is running in another thread, or it will
        deadlock.
        """
        self.__shutdown_request = True
        self.__is_shut_down.wait()
票数 6
EN

Stack Overflow用户

发布于 2010-10-05 20:20:32

否,serve_forever定期检查标志(默认情况下为0.5秒)。调用shutdown将引发此标志,并导致serve_forever结束。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3863281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档