Imagine you are in the bank waiting to be attended.
Let's say there are 10 people in the line two or three cashiers.
As soon as the first cashier finishes the next person in the line gets.
And this is exactly where I work.
Please.
It's a concurrent execution model where multiple workers are waiting for dust to be executed.
In this case the workers are the cashiers and the tasks are the people.
In this episode we will implement a simple worker pool example using the knowledge that will very have
learned from the previous episodes.
For our example we will need to create a producer first which is imagined the door in the bank so people
comes from here and gets into the line which is the channel.
Then we will create a set of workers which is exactly the same process or function that is going to
be replicated and times in order to make our program more performance.
We will increase this number.
And finally we'll have a channel out or a finish line.
Then we will have a consumer which will take messages from the channel and print them into the screen.
It's pretty similar to the example we did in the previous episode.
So probably reuse some code.
So now let's say we have the main function.
First of all we will need to create two channels in channel and the outcome.
So this is pretty similar to the previous example.
And let's do it with integrity.
Again for simplicity.
So OK we have these two channels.
The next thing we have to do is create the workers.
In this case I'll call it a co-worker because it's our worker that's going to break from the channel
and send the same message.
So let's say a co-worker and we'll have two arguments on inputs which is the internal and the output.
And I'm running this would go so that we don't block.
So let's write the function again we take an input and an output channel.
So this function basically leads the cashier.
So it's going to on the input channel invite that input into the output channel.
That's why it's on a co-worker.
Don't do really much.
OK.
Now they will run a single time.
So we want to do it forever.
So we have this right now.
We we'll simulate some execution time so we will slip for a while.
Remember that slip.
Make this function just stop for a certain amount of time and we will do again as we did previously.
We will slip for a random time.
Going gonna be random without it.
And let's say 3000 again.
So this will step from zero to three seconds.
All right.
So this is wrong.
OK.
So we're really from the channel which is in the bank example.
We're waiting for the cue when there's somebody appears we handled it.
Which is it's going to take some time and then we send it out.
OK.
So we have the worker and then we need Brawley to let's produce some messages beforehand.
Let's do in so we will produce a message into the input channel these will not get channeled as well.
It is going to be a right town so it's about being right and just forever.
Let me just send messages and also print something say Send your number.
All right.
Let's say I do this.
We need to create.
OK so we're printing this sending the message to the channel and let's increment this number to make
it change over time.
So what we're doing here is what we are just basically sending messages incrementing numbers.
So verticals that are similar than a one or two and so on.
Finally we need to consume these messages.
Which is the consumer in the drawing.
So literally right here instead of calling a function so let's read from the output channel.
Let me just play something let's say we receive the job number anything.
All right.
So this should probably be enough.
There are some simple et cetera that the way to fix yes this is Chan is member.
OK.
Now it makes us OK.
Now let's run tests and see what happens.
So we send the jobs hero do a job one and two and they are becoming to be handled we sent three messages
and we handle them in order.
So we sent 0 1 2 and we handle 0 1 2 and so on.
So one other thing that I told you in the beginning is that these are very useful algorithm to make
our program perform better.
So in this case we're just using a single worker.
But what happened is we want to create more workers so let's do it.
Let's say we want to create for workers so we can do it using a for loop.
So this is going to lunch for workers and then continue to the producer function.
So now let's analyze the different behavior when we have for workers.
I have to say let me say that first.
Yeah.
OK.
So if you compare the performance we can have a wave of processes in the same time.
Now let's go some sort of extreme let's spout 100 workers and see what happened with the output we are
able to handle.
Way more traffic.
OK.
So let me go and reduce the number of workers again so you can understand pretty much what's happening.
I move it to and now let's run it again.
So the funny thing here is that even if we're sending the jobs in order for you read the centerline
it's going to say it's 0 1 2 3 4.
But when we're receiving them in a different order because every worker takes a random amount of time
to process.
So if you see in this very example the last job that we received was the number two and it was sent
pretty much in the beginning.
So this a great way to decouple incoming traffic from the processing time.
And as I told you some very very common pattern for concurrent programming this is it for this episode
and it's going to be pretty useful for the next one which is building a distributed search engine.