[mod_python] How to use this class (Pool.py)?

jalil jalil at securia.com
Mon Feb 2 15:01:56 EST 2004


I came across the following Pool.py 
(http://dustman.net/andy/python/Pool) while reading MySQLdb docs but I 
am not sure how to use it.  I have inserted the code below. Could 
somebody familiar let me know how we are supposed to use it?

Thanks,

-Jalil

 
class Pool(Queue):
 
    """Manage a fixed-size pool of reusable, identical objects."""
    
    def __init__(self, constructor, poolsize=5):
        Queue.__init__(self, poolsize)
        self.constructor = constructor
 
    def get(self, block=1):
        """Get an object from the pool or a new one if empty."""
        try:
            return self.empty() and self.constructor() or 
Queue.get(self, block)        except Empty:
            return self.constructor()
        
    def put(self, obj, block=1):
        """Put an object into the pool if it is not full. The caller must
        not use the object after this."""
        try:
            return self.full() and None or Queue.put(self, obj, block)
        except Full:
            pass
 
 
class Constructor:
 
    """Returns a constructor that returns apply(function, args, kwargs)
    when called."""
 
    def __init__(self, function, *args, **kwargs):
        self.f = function
        self.args = args
        self.kwargs = kwargs
 
    def __call__(self):
        return apply(self.f, self.args, self.kwargs)




More information about the Mod_python mailing list