[Tutor] More assistance with queues and global variables

Kent Johnson kent37 at tds.net
Wed Jul 5 18:25:22 CEST 2006


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
>
> -Tino
>
> Example:
>
> class BaseServer:
>        def __init__(self, server_address, RequestHandlerClass):
>         """Constructor.  May be extended, do not override."""
>         self.server_address = server_address
>         self.RequestHandlerClass = RequestHandlerClass
>
>        def handle_request(self):
>         """Handle one request, possibly blocking."""
>         try:
>             request, client_address = self.get_request()
>         except socket.error:
>             return
>         if self.verify_request(request, client_address):
>             try:
>                 self.process_request(request, client_address)
>             except:
>                 self.handle_error(request, client_address)
>                 self.close_request(request)
>
>
> class TCPServer(BaseServer):
>    address_family = socket.AF_INET
>
>     socket_type = socket.SOCK_STREAM
>
>     request_queue_size = 5
>
>     allow_reuse_address = False
>
>     def __init__(self, server_address, RequestHandlerClass):
>         """Constructor.  May be extended, do not override."""
>         BaseServer.__init__(self, server_address, RequestHandlerClass)
>         self.socket = socket.socket(self.address_family ,
>                                     self.socket_type)
>         self.server_bind()
>         self.server_activate()
>
> """ Above are the classes that I will need to extend/override
>
> class MyTCPServer ( SockertServer.TCPServer):
>      """Would all the extended methods go in this class or would look 
> different? And how would I get the queue up to BaseServer without 
> overriding TCPServer's init?
>
> -Tino
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   




More information about the Tutor mailing list