I am going through Psycopg Connections Pooling documentation, and don't understand what is the purpose of 'key' argument in getconn and putconn?
Perusing the source code of psycopg2 (lib/pool.py
) you can see that the psycopg2.pool.AbstractConnectionPool
class has a dict
attribute named _used
where the connections in the pool are referenced. The key
parameter in the getconn
and putconn
methods is the the key to the items in that dictionary. By default, if the value of the key
parameter of these methods is None
the id
of the connection object is used as the key.
Basically the key
parameter allows implementers of concrete connection pool classes to identify connections. For example, in the psycopg2.pool.PersistentConnectionPool
class, a single connection is meant to be shared in a single thread, so the thread ID is used as the key.