gpt4 book ai didi

Python套接字服务器

转载 作者:太空狗 更新时间:2023-10-29 22:20:25 24 4
gpt4 key购买 nike

如何在 SocketServer 收到特定消息“退出”后调用 shutdown()?据我所知,调用 serve_forever() 会阻塞服务器。

谢谢!

最佳答案

使用来源,卢克!

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()

关于Python套接字服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3863281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com