views:

54

answers:

2

I have the following UDP server using Twisted:

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

I would like to be able to daemonize this, so from what I have read I should be doing something with a .tac file that I can start with "twistd -y my_udp_server_file.tac" - the problem is I can't find any documentation on how to do this with this kind of setup. All I can find is examples on how to daemonize simple TCP echo servers (with a .tac file, that is) - I need a multi-threaded UDP server like the one I have.

Any direction would be greatly appreciated.

+2  A: 

The daemonization code in twistd doesn't care if you're serving up UDP or TCP. The way you daemonize a UDP server is identical to the way you daemonize a TCP server. You should be able to use the TCP echo server as an example to write a .tac file for your UDP server.

Jean-Paul Calderone
+2  A: 

Try this:

import twisted.application
application = twisted.application.service.Application("Scotty's UDP server")
twisted.application.internet.UDPServer(PORT, BaseThreadedUDPServer()).setServiceParent(application)
Robie Basak
Thanks. This is what I was looking for.
Scotty