【问题标题】:Twisted UDP Server - daemonize?扭曲的 UDP 服务器 - 守护进程?
【发布时间】:2010-10-14 08:25:03
【问题描述】:

我有以下使用 Twisted 的 UDP 服务器:

# init the thread capability
threadable.init(1)

# set the thread pool size
reactor.suggestThreadPoolSize(32)

class BaseThreadedUDPServer(DatagramProtocol):
    def datagramReceived(self, datagram, (host, port)):
        #do some stuff here...

def main():
    reactor.listenUDP(PORT, BaseThreadedUDPServer())
    reactor.run()

if __name__ == '__main__':
    main()

我希望能够对此进行守护进程,因此根据我所读到的内容,我应该使用 .tac 文件做一些事情,我可以从“twistd -y my_udp_server_file.tac”开始 - 问题是我不能查找有关如何使用这种设置执行此操作的任何文档。我能找到的只是有关如何守护简单 TCP 回显服务器的示例(即使用 .tac 文件) - 我需要一个像我拥有的​​那样的多线程 UDP 服务器。

任何方向将不胜感激。

【问题讨论】:

    标签: python twisted daemon


    【解决方案1】:

    试试这个:

    import twisted.application
    application = twisted.application.service.Application("Scotty's UDP server")
    twisted.application.internet.UDPServer(PORT, BaseThreadedUDPServer()).setServiceParent(application)
    

    【讨论】:

    • 谢谢。这就是我一直在寻找的。​​span>
    【解决方案2】:

    twistd 中的守护程序代码不关心您提供的是 UDP 还是 TCP。守护 UDP 服务器的方式与守护 TCP 服务器的方式相同。您应该能够以 TCP 回显服务器为例,为您的 UDP 服务器编写一个 .tac 文件。

    【讨论】: