[Python-ideas] context managers used to allocate resources can be abused - a solution
tjreedy at udel.edu
Sun Dec 18 04:23:13 CET 2011
On 12/16/2011 9:07 PM, Nick Coghlan wrote:
> On Sat, Dec 17, 2011 at 11:00 AM, Alon Horev<alon at horev.net> wrote:
>> while I was implementing a connection pool i've noticed a pitfall of our
>> beloved with statements:
>> with pool.get_connection() as conn:
>> conn.execute(...) # the connection has been returned to the pool and does
>> not belong to the user!
> 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?
> Try the above code structure with a file object and it will give you
> "IO operation on closed file".
Because file_object.__exit__ closes the connection to the OS resource.
Terry Jan Reedy
More information about the Python-ideas