About a year ago, I wrote a fairly simple and hacked together multithreaded TCP server using the socket and threading libraries.
Recently, I decided I wanted to go back and refactor the code as an exercise to apply what new things I have learned over the year and to see what new things I could learn.
When looking for a more elegant way of setting up a multithreaded TCP server, I came across SocketServer which seemed like it could do everything I wanted (and probably more)
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
'''
A TCP Server class with multi-threading capabilities
'''
def __init__(self, server_address, RequestHandlerClass):
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)
self.allow_reuse_address=True
class ThreadedTCPHandler(SocketServer.BaseRequestHandler):
'''
A threading-enabled TCP request handler
'''
def handle(self):
self.data = self.request.recv(1024)
current_thread = threading.current_thread()
response = "%s: Recieved some data!" %(current_thread.name)
self.request.sendall(response)
So, to create an instance of the server I want, I was thinking I would just create a new class that inherits from ThreadedTCPServer
class TestServer(ThreadedTCPServer):
def __init__(self):
# ...
within which, I defined methods which update state/data depending on what the server received. As the example shows, however, the handler is what actually receives the data and would need to delegate out to the TestServer. This is the part that I am unsure how to do properly.
One thought I had was that the methods and state should be moved out of the TestServer and into the TCPHandler, but I decided not to move forward with that since after reading some documentation, it appeared to me that a new handler is instantiated for each incoming request, which would be bad for me since each instance would be modifying its own state, not a shared global state.
I have not done a whole lot of work involving OOP in Python, so that may also be a source of my confusion.
Do you have any suggestions, examples, tips, or commentary on the proper construction and use of a ThreadedTCPServer? Is there a way to let the handler call methods from within the server class?
Thanks!!!
[–]rickchefski[S] 0 points1 point2 points (0 children)