Hi - New heavily modified (but same basic idea, and drop-in replaceable) version of MiscUtils.DBPool, with the goal of creating and pruning connections based on use. I've tested this with both "unthreadsafe" (pyPgSQL) and "threadsafe" (psycopg) DBI modules.
Basic use: DBPool.__init__ now takes two new keyword arguments, "con_timeout" and "con_prune". The first sets the time in seconds after which a connection will be considered inactive, and the second sets the time in seconds between pruning runs. (Pruning is only called during getConnection, so the time may actually be longer than this.) Guts: I've modified everything somewhat. Big changes: -- both sets of functions now store PooledConnections in their connection lists, rather than storing connections and creating PooledConnections each time. This was so that I can store a timeout and reference-count in the connection object. -- _threadsafe_getConnection now searches through the list, either giving the first non-allocated connection, or if all are allocated, trying to create a new one, and finally giving the least allocated one if the other two options fail. -- _unthreadsafe_getConnection now uses a Stack (very small derivation of multithread Queue class) so that as load diminishes, connections are not handed out evenly (so that unused ones can expire). -- usedConnections() provides a non-threadsafe count of how many connections are actually in a pool. I think those are all the major changes, if you're interested please try it out or look through the code or something. Maybe include it in Webware, if enough people care. Thanks, Luke
DBPool.py
Description: Binary data
