[Python-ideas] context managers used to allocate resources can be abused - a solution

Nick Coghlan ncoghlan at gmail.com
Sun Dec 18 07:41:26 CET 2011

On Sun, Dec 18, 2011 at 1:23 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>> That's a bug in the connection pool implementation. If the underlying
>> connection has been returned to the pool, the proxy returned by the
>> context manager shouldn't work any more.
> I believe Nick is saying that conn.__exit__ should close the connection and
> release the resource, that being the point of with statements and context
> managers. Can you open a tracker issue?

No, that's not what I'm saying at all - the whole purpose of using a
connection pool is that you *don't* release the underlying OS resource
when you return a resource to the pool.

What I'm saying is that if a pool supports the context management
protocol for *temporary* access to one of the pool resources, then
*that* context manager is responsible for ensuring that the resource
reference returned is correctly invalidated in __exit__ (just as
closing a file object prevents further IO operations).

I'm not aware of any such pools in the stdlib (multiprocessing has
some pool APIs, but I don't believe they support the context
management protocol for pooled resources), so I'm suggesting that the
only problem here is in the design of the OP's own pool API.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list