[Python-ideas] context managers used to allocate resources can be abused
Alon Horev
alon at horev.net
Sat Dec 17 02:00:09 CET 2011
More information about the Python-ideas mailing list
Sat Dec 17 02:00:09 CET 2011
- Previous message: [Python-ideas] list / array comprehensions extension
- Next message: [Python-ideas] context managers used to allocate resources can be abused - a solution
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello,
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(...)
conn.execute(...) # the connection has been returned to the pool and does
not belong to the user!
a proposed solution:
with protected_context(pool.get_connection()) as conn:
....conn.execute(...)
conn.execute(...) # raises OutOfContextError()
with protected_context(file("/tmp/bla.txt", "w")) as f:
....file.write("blo")
file.write("blu") # raises OutOfContextError()
the solution is to basically proxy all methods to the real object until the
context ends and then the proxy expires.
what do you think about adding it to contextlib?
Thanks, Alon Horev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111217/bc60cb92/attachment.html>
- Previous message: [Python-ideas] list / array comprehensions extension
- Next message: [Python-ideas] context managers used to allocate resources can be abused - a solution
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list