We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Memory, Copies and Slow Code step 9 of 38
Write a Context Manager Class
Your code borrows a database connection from a pool and must give it back,
even when a query fails. A with block can do that for you. It needs a
context manager, an object that with calls at the start and the end of
a block. In the standard-library track you wrote one with
@contextmanager. Here you write the class form, which the next exercise
builds on.
with Borrowed(pool) as name:
run_query(name)
# The connection is back in the pool here, even if run_query raised.
with calls two methods:
__enter__(self)runs at the start. Its return value is whatasgives you.__exit__(self, exc_type, exc, tb)runs at the end, whether the block finished or raised. If the block raised, the three arguments describe the exception. If it finished, all three areNone.
If __exit__ returns True, Python hides the block's exception. Return
None, and the exception reaches the code around the with.
What to write
Fill in __enter__ and __exit__ in Borrowed. Keep Pool and solve as
they are.
__enter__checks out one connection withself.pool.checkout(), stores its name inself.name, and returns the name.__exit__checks that connection back in withself.pool.checkin(...), after a step that raised as well as after one that finished.__exit__returnsNone, so an error raised in the block reaches the driver.
The pool
Pool(size) starts with free connections named conn0, conn1 and so on.
checkout() takes the first free name, logs out:<name> and returns the
name. checkin(name) adds the name to the end of the free list and logs
in:<name>. With no free connection, checkout() raises
RuntimeError("no free connection").
Edge cases
- A step that raised must still give its connection back, so the next step can borrow it.
- With a pool of size 0,
checkout()raises inside__enter__. Let it raise. Python then skips the block and does not call__exit__.
What the driver reports
solve(pool_size, steps) runs each step in its own
with Borrowed(pool) as name: block. The block logs use:<name>, and a
"fail" step then raises ValueError("query failed"). The driver catches
ValueError and RuntimeError outside the block.
logis the pool's log ofout,useandinlines, in order.freeis the pool's free list at the end.errorslists each error that reached the driver, such as"ValueError: query failed".
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.