WEBVTT

1
00:00:00.130 --> 00:00:01.960
<v ->Hi guys, and welcome back.</v>

2
00:00:01.960 --> 00:00:04.600
Right now all of our models basically everywhere

3
00:00:04.600 --> 00:00:05.870
we wanna use a connection,

4
00:00:05.870 --> 00:00:07.560
we've got a bit of duplication there,

5
00:00:07.560 --> 00:00:09.790
we're actually seeing the pool directly getting

6
00:00:09.790 --> 00:00:12.600
a connection, and then putting it back when we're done.

7
00:00:12.600 --> 00:00:15.120
However, if we ever want to change the way

8
00:00:15.120 --> 00:00:17.290
that we handle the pool, for example,

9
00:00:17.290 --> 00:00:20.210
we're going to have to go everywhere and change that.

10
00:00:20.210 --> 00:00:22.620
A common approach when handling connections

11
00:00:22.620 --> 00:00:25.110
that you want to clean up when you're done is to use

12
00:00:25.110 --> 00:00:26.340
a context manager.

13
00:00:26.340 --> 00:00:28.160
We're going to be developing a context manager

14
00:00:28.160 --> 00:00:30.730
that we can use to get a connection from the pool

15
00:00:30.730 --> 00:00:32.540
and put him back when we're done.

16
00:00:32.540 --> 00:00:34.930
That way, if we later on want to change how the pool

17
00:00:34.930 --> 00:00:37.100
is implemented, we're probably gonna wanna use

18
00:00:37.100 --> 00:00:40.010
a context manager anyway, so we'll be keeping that.

19
00:00:40.010 --> 00:00:41.260
Alright, let's get to it.

20
00:00:42.130 --> 00:00:44.990
You can see that everything here has this duplication

21
00:00:44.990 --> 00:00:48.550
and now our change is going to make it a little bit simpler.

22
00:00:48.550 --> 00:00:51.680
What we're going to do is go over to connection pool.py,

23
00:00:51.680 --> 00:00:55.100
and down here we're going to create our context manager.

24
00:00:55.100 --> 00:00:57.130
If you're not familiar with context managers,

25
00:00:57.130 --> 00:00:59.370
then we've got a resource linked over in

26
00:00:59.370 --> 00:01:00.970
the resources section of this lecture

27
00:01:00.970 --> 00:01:02.490
that you can check out.

28
00:01:02.490 --> 00:01:05.040
You should also be familiar with the yield keyword

29
00:01:05.040 --> 00:01:06.200
in Python.

30
00:01:06.200 --> 00:01:07.440
If you're not familiar with that,

31
00:01:07.440 --> 00:01:09.830
also check out the resources section.

32
00:01:09.830 --> 00:01:11.950
Let's create our context manager.

33
00:01:11.950 --> 00:01:13.960
The first thing we have to do is go ahead

34
00:01:13.960 --> 00:01:16.500
and import something from the standard library.

35
00:01:16.500 --> 00:01:20.570
We do from context lib, import context manager.

36
00:01:20.570 --> 00:01:23.040
And what this does is it turns any function into

37
00:01:23.040 --> 00:01:24.700
a context manager.

38
00:01:24.700 --> 00:01:26.994
So we can do @contextmanager,

39
00:01:26.994 --> 00:01:28.160
this is a decorator by the way,

40
00:01:28.160 --> 00:01:30.200
and so if you're not familiar with decorators,

41
00:01:30.200 --> 00:01:31.610
also we've got something linked in

42
00:01:31.610 --> 00:01:33.400
the resources section of this lecture.

43
00:01:33.400 --> 00:01:35.640
Then this is gonna be called get connection.

44
00:01:35.640 --> 00:01:38.150
And what this does is it gets a connection from the pool,

45
00:01:38.150 --> 00:01:40.120
with pool, get con.

46
00:01:40.120 --> 00:01:42.970
And then it's going to do something,

47
00:01:42.970 --> 00:01:46.100
is going to get to that connection

48
00:01:46.100 --> 00:01:51.100
and the return it but it's going to return it using yield.

49
00:01:52.300 --> 00:01:56.850
Then when it receives the control back from the caller,

50
00:01:56.850 --> 00:01:59.930
we're going to use a finally block here

51
00:01:59.930 --> 00:02:03.470
to put the connection back into the pool.

52
00:02:03.470 --> 00:02:08.110
So quick primer on what a yield does,

53
00:02:08.110 --> 00:02:10.790
we're going to be calling this as

54
00:02:10.790 --> 00:02:13.870
a context manager using the width keyword.

55
00:02:13.870 --> 00:02:15.650
And what that's gonna do is it's going

56
00:02:15.650 --> 00:02:19.140
to run this function up to the yield,

57
00:02:19.140 --> 00:02:21.260
then is going to return connection

58
00:02:21.260 --> 00:02:23.380
and give control to the caller.

59
00:02:23.380 --> 00:02:25.220
When the context manager is done,

60
00:02:25.220 --> 00:02:26.850
we're going to come back to the yield,

61
00:02:26.850 --> 00:02:29.470
and we're gonna run the finally block.

62
00:02:29.470 --> 00:02:31.640
So again, you do have to be familiar with yield

63
00:02:31.640 --> 00:02:33.950
and context managers for this to really make sense.

64
00:02:33.950 --> 00:02:35.650
But we've got some resources linked

65
00:02:35.650 --> 00:02:38.190
in the resources section of this lecture.

66
00:02:38.190 --> 00:02:40.470
Now that we've got this, we can go over to pull.py,

67
00:02:40.470 --> 00:02:42.770
and instead of importing the pool here,

68
00:02:42.770 --> 00:02:45.000
we're gonna import get connection.

69
00:02:45.000 --> 00:02:49.920
And now we can do with get connection as connection.

70
00:02:49.920 --> 00:02:51.380
And we no longer need that.

71
00:02:51.380 --> 00:02:54.960
We need a colon and we can put that in there.

72
00:02:54.960 --> 00:02:57.060
We don't need to put the connection back.

73
00:02:57.060 --> 00:02:59.800
But we do need to have this in there as well.

74
00:02:59.800 --> 00:03:02.810
So what happens now is and just go through it again,

75
00:03:02.810 --> 00:03:07.610
we run get connection, this goes over here and runs up

76
00:03:07.610 --> 00:03:10.550
to the yield, it returns connection.

77
00:03:10.550 --> 00:03:13.910
But it essentially pauses this function.

78
00:03:13.910 --> 00:03:15.790
Connection then becomes the value of

79
00:03:15.790 --> 00:03:17.560
this connection variable.

80
00:03:17.560 --> 00:03:19.780
We use it in this block.

81
00:03:19.780 --> 00:03:21.810
And when we reach the end of the block,

82
00:03:21.810 --> 00:03:25.270
the yield resumes, and we run the finally,

83
00:03:25.270 --> 00:03:27.683
which puts the connection back into the pool.

84
00:03:28.970 --> 00:03:31.660
So that's essentially how a yield keyword works.

85
00:03:31.660 --> 00:03:33.510
But again, more information in the resources section

86
00:03:33.510 --> 00:03:35.020
of this lecture.

87
00:03:35.020 --> 00:03:37.890
So we can copy this and use it everywhere

88
00:03:37.890 --> 00:03:39.510
that we're getting a connection,

89
00:03:39.510 --> 00:03:42.450
and that is gonna simplify our approach a little bit.

90
00:03:42.450 --> 00:03:45.190
We no longer need to put the connections back

91
00:03:45.190 --> 00:03:46.690
because that's happening for us.

92
00:03:46.690 --> 00:03:49.763
And we put the rest of the stuff in the context manager.

93
00:03:50.790 --> 00:03:52.460
Notice that we can put return statements

94
00:03:52.460 --> 00:03:53.760
inside the context manager.

95
00:03:53.760 --> 00:03:54.640
That's okay.

96
00:03:54.640 --> 00:03:57.040
Because we've got this inside a finally block

97
00:03:57.040 --> 00:03:58.730
is always gonna run.

98
00:03:58.730 --> 00:04:02.340
Alright, let's go and do this over an option.py as well.

99
00:04:02.340 --> 00:04:05.460
I'm gonna copy this there, and we're gonna put it in there,

100
00:04:05.460 --> 00:04:07.010
then I'm gonna come back to pull.py

101
00:04:07.010 --> 00:04:10.140
and copy this context manager and use it everywhere,

102
00:04:10.140 --> 00:04:11.700
as well.

103
00:04:11.700 --> 00:04:14.560
Notice that this does decrease our code length

104
00:04:14.560 --> 00:04:16.700
by essentially one line per function.

105
00:04:16.700 --> 00:04:20.990
But also, it makes it so that all the interactions with

106
00:04:20.990 --> 00:04:24.120
the pool happen in the context manager.

107
00:04:24.120 --> 00:04:27.270
If we ever want to change how we're using the pool,

108
00:04:27.270 --> 00:04:28.610
or how we're connecting,

109
00:04:28.610 --> 00:04:31.150
or whether we're using psychopg2 at all,

110
00:04:31.150 --> 00:04:33.630
all we have to do is change the context manager.

111
00:04:33.630 --> 00:04:34.840
And like I said earlier on,

112
00:04:34.840 --> 00:04:37.480
using a context manager to get things

113
00:04:37.480 --> 00:04:39.680
and clean up after yourself afterwards,

114
00:04:39.680 --> 00:04:42.030
is so common in Python that we'll probably wanna do

115
00:04:42.030 --> 00:04:42.943
this anyway.

116
00:04:44.490 --> 00:04:46.470
The last place to use the context manager

117
00:04:46.470 --> 00:04:48.130
is over in app.py.

118
00:04:48.130 --> 00:04:50.520
So here instead of getting and putting the connection back,

119
00:04:50.520 --> 00:04:52.520
we're gonna do the same thing.

120
00:04:52.520 --> 00:04:55.180
Putting that in there and now we can go back to the top

121
00:04:55.180 --> 00:04:57.750
and use this instead.

122
00:04:57.750 --> 00:04:59.350
Alright, thank you guys for watching this video.

123
00:04:59.350 --> 00:05:00.183
Thanks for joining me.

124
00:05:00.183 --> 00:05:01.190
I hope you've learnt something

125
00:05:01.190 --> 00:05:02.840
and I'll see you in the next one.

