Tino Dai wrote:
> Hi All,
>
> My project is almost working (and without global variables!), and
> there is one more hurdle that I want to jump. That is using the
> SocketServer module and passing a queue (or for that matter any kind
> of variable) to the handle method. I took a look at SocketServer to
> see what classes I need to override/extend, and see that I will be
> need to extending a couple of inits and methods out of the TCPServer
> and the BaseServer class. I don't see an easy way to do this [passing
> in a variable] , so I figured that I would extend the classes. I can
> to realize that I would be extending not only the base TCPServer class
> but also the BaseServer class. My question is: When I extend those
> classes, can I put all the methods from both of the classes into my
> own extended class or would I need to do something funky?
If I understand correctly, you want the server to have a queue that is
shared among requests. You can do this without replacing any of the
functions below. I would pass the queue to the __init__() method of your
server class, override finish_request() to pass the queue to the request
handler, the request handler __init__() just saves the queue for access
by the handle() method. Something like this:
class MyServer(TCPServer):
def __init__(self, server_address, RequestHandlerClass, queue):
TCPServer.__init__(self, server_address, RequestHandlerClass)
self.queue = queue
def finish_request(self, request, client_address):
"""Finish one request by instantiating RequestHandlerClass."""
self.RequestHandlerClass(request, client_address, self, self.queue)
class MyRequest(BaseRequestHandler):
def __init__(self, request, client_address, server, queue):
BaseRequestHandler.__init__(self, request, client_address, server)
self.queue = queue
def handle(self):
# do something with self.queue
Kent
Do you ever wish that you could pull back an email? This is one of those times. I got the same exact solution as you about 30 minutes ago. I did have to switch the lines in BaseRequestHandler and self.queue for it to work. Thanks!
-Tino
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
