6

特定のメッセージ「exit」を受信shutdown()した後、どのように呼び出すことができますか? SocketServer私が知っているように、への呼び出しserve_forever()はサーバーをブロックします。

ありがとう!

4

2 に答える 2

6

ソースを使え、ルーク!

SocketServer.py からの抜粋:

   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()
于 2010-10-05T14:37:56.950 に答える
4

いいえ、serve_forever定期的にフラグをチェックしています (デフォルトでは 0.5 秒)。shutdown を呼び出すと、このフラグが発生し、serve_forever が終了します。

于 2010-10-05T12:20:31.653 に答える