Skip to content Skip to sidebar Skip to footer

Python Threads Passing Parameters

I'm trying to passing some parameter to the server tread, but I have no idea how? this is my code: HOST, PORT = socket.gethostbyname( socket.gethostname() ), 31000 self.server = So

Solution 1:

I think the second parameter of ThreadingTCPServer is a factory:

  SocketServer.ThreadingTCPServer( ( HOST, PORT ), MCRequestHandler )

What you could do is your own factory here. Class will contstuct a callable object. when the object is called it will initialize MCRequestHandler with parameters given to the factory:

classMyRequestHandlerFactory(object):def__init__(self, param1, param2): 
            self.param1 = param1
            self.param2 = param2

  def__call__(self):
            handler = MCRequestHandler()
            handler.param1 = param1
            handler.param2 = param2

Then initialize:

  factory = MyRequestHandlerFactory("x", "y")
  SocketServer.ThreadingTCPServer( ( HOST, PORT ), factory)

Post a Comment for "Python Threads Passing Parameters"