停止 UDP 套接字服务器

发布于 2025-01-02 04:07:38 字数 671 浏览 4 评论 0原文

我正在创建一个 UDP SocketServer,如果我从客户端收到关闭消息,我想关闭,而不是关闭 UDP 连接。

当我收到消息时,将显示 Begin,但不显示 Finished。我怎样才能退出这个线程?

class MyUDPHandler( SocketServer.BaseRequestHandler ):       
    def handle( self ):
        data = self.request[0].strip()        
        dic = self.string_XML_to_dic( data )
        if( dic['Cmd'] == str(UDPConst().SHUT_DOWN )): 
            print('Begin')           
            self.server.shutdown()
            print("Finished")

if __name__ == "__main__": 
    HOST, PORT = "", prop['udpport']
    server = SocketServer.UDPServer( ( HOST, PORT ), MyUDPHandler )    
    server.serve_forever()

I'm creating an UDP SocketServer, and I would like to shutdown if I'm receiving a close message from the client side than I would like to close the UDP connection.

As I received the message the Begin will be displayed, but the Finished is not displayed. How can I exit from this thread?

class MyUDPHandler( SocketServer.BaseRequestHandler ):       
    def handle( self ):
        data = self.request[0].strip()        
        dic = self.string_XML_to_dic( data )
        if( dic['Cmd'] == str(UDPConst().SHUT_DOWN )): 
            print('Begin')           
            self.server.shutdown()
            print("Finished")

if __name__ == "__main__": 
    HOST, PORT = "", prop['udpport']
    server = SocketServer.UDPServer( ( HOST, PORT ), MyUDPHandler )    
    server.serve_forever()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2025-01-09 04:07:38

由于您没有在自己的线程中运行服务器(为此您需要继承 ThreadingMixIn ),因此您不能使用 shutdown ,因为它会导致死锁。从中的函数文档注释:

阻塞直到循环完成。这必须在调用时调用
serve_forever() 正在另一个线程中运行,否则它将
陷入僵局。

As you are not running the server in it's own thread (you need to inherit ThreadingMixIn for that), you can not use shutdown as it will cause a deadlock. From the function document comment in the source:

Blocks until the loop has finished. This must be called while
serve_forever() is running in another thread, or it will
deadlock.

你与昨日 2025-01-09 04:07:38
# Step 1 - Create a global object
global_object = dict()

# Step 2 - Create a function that will receive kill, Control-C signals
def stop_listener(*args): 
   global_object['listener_server'].shutdown()

# Step 3 - Assign kill, and Control-C signal to function=stop_listener
import signal
signal.signal(signal.SIGINT , stop_listener)
signal.signal(signal.SIGTERM, stop_listener)

# Step 4 - Create UDP Server in usual way, and save a reference in global_object
server = socketserver.UDPServer(...)
global_object['listener_server'] = server
server.serve_forever()

说明:

  • 当按下 Control-C 或当向 pid -- 函数发送kill时
    = stop_listener 被调用。
  • function=stop_listener,然后在 UDPServer 引用上调用 shutdown。 -
  • 这有助于清晰的听众。
# Step 1 - Create a global object
global_object = dict()

# Step 2 - Create a function that will receive kill, Control-C signals
def stop_listener(*args): 
   global_object['listener_server'].shutdown()

# Step 3 - Assign kill, and Control-C signal to function=stop_listener
import signal
signal.signal(signal.SIGINT , stop_listener)
signal.signal(signal.SIGTERM, stop_listener)

# Step 4 - Create UDP Server in usual way, and save a reference in global_object
server = socketserver.UDPServer(...)
global_object['listener_server'] = server
server.serve_forever()

Explanation:

  • When Control-C is pressed or when kill is sent to pid -- function
    = stop_listener gets called.
  • function=stop_listener, then calls shutdown on the UDPServer reference. -
  • this helps clear listener.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文