WEBVTT 00:01.320 --> 00:03.710 Imagine you are in the bank waiting to be attended. 00:04.020 --> 00:07.650 Let's say there are 10 people in the line two or three cashiers. 00:07.890 --> 00:12.190 As soon as the first cashier finishes the next person in the line gets. 00:12.670 --> 00:14.230 And this is exactly where I work. 00:14.230 --> 00:15.320 Please. 00:15.360 --> 00:21.510 It's a concurrent execution model where multiple workers are waiting for dust to be executed. 00:21.510 --> 00:27.920 In this case the workers are the cashiers and the tasks are the people. 00:27.920 --> 00:34.050 In this episode we will implement a simple worker pool example using the knowledge that will very have 00:34.050 --> 00:36.640 learned from the previous episodes. 00:36.870 --> 00:43.620 For our example we will need to create a producer first which is imagined the door in the bank so people 00:43.620 --> 00:47.550 comes from here and gets into the line which is the channel. 00:47.940 --> 00:53.790 Then we will create a set of workers which is exactly the same process or function that is going to 00:53.790 --> 00:58.460 be replicated and times in order to make our program more performance. 00:58.470 --> 00:59.960 We will increase this number. 01:00.270 --> 01:04.530 And finally we'll have a channel out or a finish line. 01:04.890 --> 01:11.670 Then we will have a consumer which will take messages from the channel and print them into the screen. 01:11.700 --> 01:15.640 It's pretty similar to the example we did in the previous episode. 01:15.950 --> 01:17.700 So probably reuse some code. 01:18.150 --> 01:22.680 So now let's say we have the main function. 01:22.680 --> 01:27.160 First of all we will need to create two channels in channel and the outcome. 01:27.390 --> 01:30.160 So this is pretty similar to the previous example. 01:30.300 --> 01:32.180 And let's do it with integrity. 01:32.190 --> 01:33.970 Again for simplicity. 01:33.970 --> 01:36.030 So OK we have these two channels. 01:36.090 --> 01:39.120 The next thing we have to do is create the workers. 01:39.150 --> 01:44.040 In this case I'll call it a co-worker because it's our worker that's going to break from the channel 01:44.400 --> 01:45.620 and send the same message. 01:45.960 --> 01:55.890 So let's say a co-worker and we'll have two arguments on inputs which is the internal and the output. 01:55.890 --> 01:59.900 And I'm running this would go so that we don't block. 02:00.420 --> 02:06.270 So let's write the function again we take an input and an output channel. 02:07.440 --> 02:10.640 So this function basically leads the cashier. 02:10.670 --> 02:18.200 So it's going to on the input channel invite that input into the output channel. 02:18.260 --> 02:19.940 That's why it's on a co-worker. 02:20.060 --> 02:21.410 Don't do really much. 02:21.530 --> 02:22.020 OK. 02:22.110 --> 02:24.230 Now they will run a single time. 02:24.230 --> 02:26.480 So we want to do it forever. 02:26.600 --> 02:30.000 So we have this right now. 02:30.090 --> 02:34.790 We we'll simulate some execution time so we will slip for a while. 02:34.820 --> 02:36.070 Remember that slip. 02:36.080 --> 02:44.680 Make this function just stop for a certain amount of time and we will do again as we did previously. 02:44.690 --> 02:46.690 We will slip for a random time. 02:46.760 --> 02:49.740 Going gonna be random without it. 02:50.420 --> 02:52.850 And let's say 3000 again. 02:53.120 --> 02:56.890 So this will step from zero to three seconds. 02:56.930 --> 02:58.310 All right. 02:58.430 --> 03:00.350 So this is wrong. 03:00.420 --> 03:00.660 OK. 03:00.890 --> 03:04.550 So we're really from the channel which is in the bank example. 03:04.550 --> 03:09.360 We're waiting for the cue when there's somebody appears we handled it. 03:09.650 --> 03:13.340 Which is it's going to take some time and then we send it out. 03:14.030 --> 03:14.610 OK. 03:14.660 --> 03:22.220 So we have the worker and then we need Brawley to let's produce some messages beforehand. 03:23.240 --> 03:32.650 Let's do in so we will produce a message into the input channel these will not get channeled as well. 03:33.590 --> 03:43.020 It is going to be a right town so it's about being right and just forever. 03:43.120 --> 03:52.800 Let me just send messages and also print something say Send your number. 03:52.800 --> 03:53.740 All right. 03:54.260 --> 03:56.720 Let's say I do this. 03:56.720 --> 03:57.910 We need to create. 03:58.020 --> 04:07.820 OK so we're printing this sending the message to the channel and let's increment this number to make 04:07.820 --> 04:09.080 it change over time. 04:09.080 --> 04:15.200 So what we're doing here is what we are just basically sending messages incrementing numbers. 04:15.320 --> 04:19.070 So verticals that are similar than a one or two and so on. 04:19.550 --> 04:21.520 Finally we need to consume these messages. 04:21.560 --> 04:24.500 Which is the consumer in the drawing. 04:24.720 --> 04:33.240 So literally right here instead of calling a function so let's read from the output channel. 04:33.260 --> 04:41.070 Let me just play something let's say we receive the job number anything. 04:41.070 --> 04:41.590 All right. 04:41.770 --> 04:46.790 So this should probably be enough. 04:46.870 --> 04:53.550 There are some simple et cetera that the way to fix yes this is Chan is member. 04:53.710 --> 04:54.090 OK. 04:54.130 --> 04:56.050 Now it makes us OK. 04:56.140 --> 04:58.190 Now let's run tests and see what happens. 04:58.960 --> 05:07.180 So we send the jobs hero do a job one and two and they are becoming to be handled we sent three messages 05:07.510 --> 05:08.920 and we handle them in order. 05:09.190 --> 05:15.550 So we sent 0 1 2 and we handle 0 1 2 and so on. 05:15.910 --> 05:22.060 So one other thing that I told you in the beginning is that these are very useful algorithm to make 05:22.060 --> 05:23.910 our program perform better. 05:23.920 --> 05:27.620 So in this case we're just using a single worker. 05:27.780 --> 05:32.020 But what happened is we want to create more workers so let's do it. 05:32.020 --> 05:38.350 Let's say we want to create for workers so we can do it using a for loop. 05:38.860 --> 05:45.220 So this is going to lunch for workers and then continue to the producer function. 05:45.220 --> 05:50.240 So now let's analyze the different behavior when we have for workers. 05:50.360 --> 05:53.700 I have to say let me say that first. 05:53.790 --> 05:54.090 Yeah. 05:54.190 --> 05:54.650 OK. 05:54.730 --> 06:01.820 So if you compare the performance we can have a wave of processes in the same time. 06:01.840 --> 06:11.950 Now let's go some sort of extreme let's spout 100 workers and see what happened with the output we are 06:11.950 --> 06:13.060 able to handle. 06:13.270 --> 06:14.490 Way more traffic. 06:14.650 --> 06:14.960 OK. 06:14.990 --> 06:21.670 So let me go and reduce the number of workers again so you can understand pretty much what's happening. 06:21.670 --> 06:24.550 I move it to and now let's run it again. 06:25.480 --> 06:31.380 So the funny thing here is that even if we're sending the jobs in order for you read the centerline 06:31.580 --> 06:34.510 it's going to say it's 0 1 2 3 4. 06:35.170 --> 06:41.500 But when we're receiving them in a different order because every worker takes a random amount of time 06:41.500 --> 06:42.690 to process. 06:42.730 --> 06:51.500 So if you see in this very example the last job that we received was the number two and it was sent 06:51.640 --> 06:53.120 pretty much in the beginning. 06:53.140 --> 06:59.180 So this a great way to decouple incoming traffic from the processing time. 06:59.290 --> 07:05.650 And as I told you some very very common pattern for concurrent programming this is it for this episode 07:06.190 --> 07:12.580 and it's going to be pretty useful for the next one which is building a distributed search engine.