[英]Python SocketServer
How can I call shutdown()
in a SocketServer
after receiving a certain message "exit"? 在收到某个消息“exit”后,如何在
SocketServer
调用shutdown()
? As I know, the call to serve_forever()
will block the server. 据我所知,对
serve_forever()
的调用将阻止服务器。
Thanks! 谢谢!
Use the source, Luke! 使用来源,卢克!
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()
No the serve_forever
is checking a flag on a regular basis (by default 0.5 sec). 没有
serve_forever
定期检查标志(默认为0.5秒)。 Calling shutdown will raise this flag and cause the serve_forever to end. 调用shutdown将引发此标志并导致serve_forever结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.