-: Hi guys, and welcome back.
Right now all of our models basically everywhere
we wanna use a connection,
we've got a bit of duplication there,
we're actually seeing the pool directly getting
a connection, and then putting it back when we're done.
However, if we ever want to change the way
that we handle the pool, for example,
we're going to have to go everywhere and change that.
A common approach when handling connections
that you want to clean up when you're done is to use
a context manager.
We're going to be developing a context manager
that we can use to get a connection from the pool
and put him back when we're done.
That way, if we later on want to change how the pool
is implemented, we're probably gonna wanna use
a context manager anyway, so we'll be keeping that.
Alright, let's get to it.
You can see that everything here has this duplication
and now our change is going to make it a little bit simpler.
What we're going to do is go over to connection pool.py,
and down here we're going to create our context manager.
If you're not familiar with context managers,
then we've got a resource linked over in
the resources section of this lecture
that you can check out.
You should also be familiar with the yield keyword
in Python.
If you're not familiar with that,
also check out the resources section.
Let's create our context manager.
The first thing we have to do is go ahead
and import something from the standard library.
We do from context lib, import context manager.
And what this does is it turns any function into
a context manager.
So we can do @contextmanager,
this is a decorator by the way,
and so if you're not familiar with decorators,
also we've got something linked in
the resources section of this lecture.
Then this is gonna be called get connection.
And what this does is it gets a connection from the pool,
with pool, get con.
And then it's going to do something,
is going to get to that connection
and the return it but it's going to return it using yield.
Then when it receives the control back from the caller,
we're going to use a finally block here
to put the connection back into the pool.
So quick primer on what a yield does,
we're going to be calling this as
a context manager using the width keyword.
And what that's gonna do is it's going
to run this function up to the yield,
then is going to return connection
and give control to the caller.
When the context manager is done,
we're going to come back to the yield,
and we're gonna run the finally block.
So again, you do have to be familiar with yield
and context managers for this to really make sense.
But we've got some resources linked
in the resources section of this lecture.
Now that we've got this, we can go over to pull.py,
and instead of importing the pool here,
we're gonna import get connection.
And now we can do with get connection as connection.
And we no longer need that.
We need a colon and we can put that in there.
We don't need to put the connection back.
But we do need to have this in there as well.
So what happens now is and just go through it again,
we run get connection, this goes over here and runs up
to the yield, it returns connection.
But it essentially pauses this function.
Connection then becomes the value of
this connection variable.
We use it in this block.
And when we reach the end of the block,
the yield resumes, and we run the finally,
which puts the connection back into the pool.
So that's essentially how a yield keyword works.
But again, more information in the resources section
of this lecture.
So we can copy this and use it everywhere
that we're getting a connection,
and that is gonna simplify our approach a little bit.
We no longer need to put the connections back
because that's happening for us.
And we put the rest of the stuff in the context manager.
Notice that we can put return statements
inside the context manager.
That's okay.
Because we've got this inside a finally block
is always gonna run.
Alright, let's go and do this over an option.py as well.
I'm gonna copy this there, and we're gonna put it in there,
then I'm gonna come back to pull.py
and copy this context manager and use it everywhere,
as well.
Notice that this does decrease our code length
by essentially one line per function.
But also, it makes it so that all the interactions with
the pool happen in the context manager.
If we ever want to change how we're using the pool,
or how we're connecting,
or whether we're using psychopg2 at all,
all we have to do is change the context manager.
And like I said earlier on,
using a context manager to get things
and clean up after yourself afterwards,
is so common in Python that we'll probably wanna do
this anyway.
The last place to use the context manager
is over in app.py.
So here instead of getting and putting the connection back,
we're gonna do the same thing.
Putting that in there and now we can go back to the top
and use this instead.
Alright, thank you guys for watching this video.
Thanks for joining me.
I hope you've learnt something
and I'll see you in the next one.