Reputation: 5870
I'm using TCPHandler to precess TCP requests from client sockets, but I encountered the performance issue when I was using multiThreading, the snippet code as following:
class TCPHandler(SocketServer.StreamRequestHandler):
def handle(self):
.............
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
if __name__=="__main__":
server = ThreadedTCPServer((host, port), TCPHandler)
server.serve_forever()
The above code is multipleThreading, how can I convert it to multipleProcessing.
Upvotes: 0
Views: 1208
Reputation: 43495
Replace SocketServer.ThreadingMixIn
with SocketServer.ForkingMixIn
to make your current code spawn a process to handle a request. Note that this might not be very efficient, because starting a process takes time and resources. If the time needed to handle the request is shorter than the time necessary to start up a new process, it might make things slower.
Depending on what you are doing, an event-driven architecture might be more suitable. For Python 2 you might want to look at the asyncore
and asynchat
modules from the standard library. Or the twisted framework. Note that an event-driven architecture is completely different from what you have now, and would probably require a rewrite.
Upvotes: 1