Press "Enter" to skip to content

Goroutines – Go Lang Practical Programming Tutorial p.18


what’s going on everybody welcome to

what’s going on everybody welcome to another go language programming tutorial

another go language programming tutorial

another go language programming tutorial video in this video what we’re gonna be

video in this video what we’re gonna be

video in this video what we’re gonna be talking about is concurrency or at least

talking about is concurrency or at least

talking about is concurrency or at least we’re gonna start talking about

we’re gonna start talking about

we’re gonna start talking about concurrency in the go programming

concurrency in the go programming

concurrency in the go programming language now first what we should talk

language now first what we should talk

language now first what we should talk about is what exactly is concurrency

about is what exactly is concurrency

about is what exactly is concurrency because this can be kind of confusing

because this can be kind of confusing

because this can be kind of confusing so concurrency is not in parallel the

so concurrency is not in parallel the

so concurrency is not in parallel the way you can think about it is

way you can think about it is

way you can think about it is concurrency is like dealing with

concurrency is like dealing with

concurrency is like dealing with multiple things at any one time

multiple things at any one time

multiple things at any one time whereas parallel parallelism is doing

whereas parallel parallelism is doing

whereas parallel parallelism is doing multiple things at any one time so for

multiple things at any one time so for

multiple things at any one time so for example concurrency is like consider

example concurrency is like consider

example concurrency is like consider your like a CEO of a company you’re

your like a CEO of a company you’re

your like a CEO of a company you’re writing a company-wide memo but as

writing a company-wide memo but as

writing a company-wide memo but as emails come in you’re responding emails

emails come in you’re responding emails

emails come in you’re responding emails maybe someone comes to your door and

maybe someone comes to your door and

maybe someone comes to your door and asks a quick question you you pause the

asks a quick question you you pause the

asks a quick question you you pause the memo and you answer that question and

memo and you answer that question and

memo and you answer that question and then you get back to the memo something

then you get back to the memo something

then you get back to the memo something like that you’re doing those things

like that you’re doing those things

like that you’re doing those things concurrently so that way when that

concurrently so that way when that

concurrently so that way when that person comes to your door they don’t

person comes to your door they don’t

person comes to your door they don’t have to like stand there until you’re

have to like stand there until you’re

have to like stand there until you’re finished with the memo necessarily or

finished with the memo necessarily or

finished with the memo necessarily or something like that so in our case like

something like that so in our case like

something like that so in our case like with our web app we’re spending a lot of

with our web app we’re spending a lot of

with our web app we’re spending a lot of time just sitting there we’re sending

time just sitting there we’re sending

time just sitting there we’re sending out the requests we’re waiting for the

out the requests we’re waiting for the

out the requests we’re waiting for the requests to come back that’s a lot at a

requests to come back that’s a lot at a

requests to come back that’s a lot at a time it’s like 50 to 75 milliseconds

time it’s like 50 to 75 milliseconds

time it’s like 50 to 75 milliseconds multiply that by every single site map

multiply that by every single site map

multiply that by every single site map that we’re visiting and they just keep

that we’re visiting and they just keep

that we’re visiting and they just keep adding up whereas we actually could be

adding up whereas we actually could be

adding up whereas we actually could be while we’re waiting for one thing to

while we’re waiting for one thing to

while we’re waiting for one thing to make a response we actually could send

make a response we actually could send

make a response we actually could send out another request or waiting for that

out another request or waiting for that

out another request or waiting for that also to make a response from sound on

also to make a response from sound on

also to make a response from sound on another request chances are we could

another request chances are we could

another request chances are we could probably send out all our requests in 75

probably send out all our requests in 75

probably send out all our requests in 75 milliseconds so so that’s that’s

milliseconds so so that’s that’s

milliseconds so so that’s that’s concurrency so in go the way we do

concurrency so in go the way we do

concurrency so in go the way we do concurrency is with go routines the way

concurrency is with go routines the way

concurrency is with go routines the way we use a go routine is you just simply

we use a go routine is you just simply

we use a go routine is you just simply type go I have no idea where it is but

type go I have no idea where it is but

type go I have no idea where it is but anyways type go in front of the function

anyways type go in front of the function

anyways type go in front of the function call so let’s just make a quick example

call so let’s just make a quick example

call so let’s just make a quick example to illustrate this so I’m just going to

to illustrate this so I’m just going to

to illustrate this so I’m just going to say package main and then we’re gonna go

say package main and then we’re gonna go

say package main and then we’re gonna go ahead and just we’re gonna make a couple

ahead and just we’re gonna make a couple

ahead and just we’re gonna make a couple imports we’re going to import time and

imports we’re going to import time and

imports we’re going to import time and format just to print things out and then

format just to print things out and then

format just to print things out and then let’s say we’ve got a function that says

let’s say we’ve got a function that says

let’s say we’ve got a function that says whatever string we pass and that’s all

whatever string we pass and that’s all

whatever string we pass and that’s all it does it just says whatever so in this

it does it just says whatever so in this

it does it just says whatever so in this case and in fact let’s make it a little

case and in fact let’s make it a little

case and in fact let’s make it a little more complicated so let’s say

more complicated so let’s say

more complicated so let’s say iterates like it says the thing like

iterates like it says the thing like

iterates like it says the thing like three times so let’s say for I : equals

three times so let’s say for I : equals

three times so let’s say for I : equals zero while I is less than three and then

zero while I is less than three and then

zero while I is less than three and then I increment let’s just format dot print

I increment let’s just format dot print

I increment let’s just format dot print line s and then also let’s go ahead and

line s and then also let’s go ahead and

line s and then also let’s go ahead and do a time die capital s sleep time diet

do a time die capital s sleep time diet

do a time die capital s sleep time diet millisecond times 100 so okay cool I

millisecond times 100 so okay cool I

millisecond times 100 so okay cool I mean this should be totally clear like

mean this should be totally clear like

mean this should be totally clear like what’s going on in this function so

what’s going on in this function so

what’s going on in this function so hopefully you understand now let’s do it

hopefully you understand now let’s do it

hopefully you understand now let’s do it go ahead and make our main and then our

go ahead and make our main and then our

go ahead and make our main and then our main function

main function

main function if we will like let’s say we’re gonna

if we will like let’s say we’re gonna

if we will like let’s say we’re gonna say two things let’s say we’re gonna say

say two things let’s say we’re gonna say

say two things let’s say we’re gonna say say hey and then say there so let’s save

say hey and then say there so let’s save

say hey and then say there so let’s save that come up here and then go run go go

that come up here and then go run go go

that come up here and then go run go go and we see hey hey hey

and we see hey hey hey

and we see hey hey hey there there there everything kind of ran

there there there everything kind of ran

there there there everything kind of ran you know in parallel here or not

you know in parallel here or not

you know in parallel here or not parallel

parallel

parallel ran linearly rather so when we got to

ran linearly rather so when we got to

ran linearly rather so when we got to this it ran each of the iterations

this it ran each of the iterations

this it ran each of the iterations before it got to this line now instead

before it got to this line now instead

before it got to this line now instead what if we converted one of these things

what if we converted one of these things

what if we converted one of these things to a goroutine

to a goroutine

to a goroutine for example let’s just convert the first

for example let’s just convert the first

for example let’s just convert the first one to a go routine so we just throw go

one to a go routine so we just throw go

one to a go routine so we just throw go the ghost statement right in front of

the ghost statement right in front of

the ghost statement right in front of the function that’s it so a go routine

the function that’s it so a go routine

the function that’s it so a go routine and go is just like a lightweight thread

and go is just like a lightweight thread

and go is just like a lightweight thread okay so it’s gonna launch say hey in a

okay so it’s gonna launch say hey in a

okay so it’s gonna launch say hey in a thread for us and that’s it that’s all

thread for us and that’s it that’s all

thread for us and that’s it that’s all you have to do to to it so let’s go

you have to do to to it so let’s go

you have to do to to it so let’s go ahead and run that really quick and here

ahead and run that really quick and here

ahead and run that really quick and here we can see it’s just okay this time it

we can see it’s just okay this time it

we can see it’s just okay this time it actually said there before hey I won’t

actually said there before hey I won’t

actually said there before hey I won’t necessarily I don’t think it wouldn’t

necessarily I don’t think it wouldn’t

necessarily I don’t think it wouldn’t necessarily always do that but anyway it

necessarily always do that but anyway it

necessarily always do that but anyway it might have maybe it would actually for a

might have maybe it would actually for a

might have maybe it would actually for a reason I’m gonna explain in a moment so

reason I’m gonna explain in a moment so

reason I’m gonna explain in a moment so what if we made both of these go regions

what if we made both of these go regions

what if we made both of these go regions so we say go say hey go say there so

so we say go say hey go say there so

so we say go say hey go say there so what if we made them both go routines

what if we made them both go routines

what if we made them both go routines what do you what would happen so let’s

what do you what would happen so let’s

what do you what would happen so let’s go ahead and run it real quick and

go ahead and run it real quick and

go ahead and run it real quick and suddenly we see nothing what if what if

suddenly we see nothing what if what if

suddenly we see nothing what if what if we said

we said

we said like let’s say we do say hey and then go

like let’s say we do say hey and then go

like let’s say we do say hey and then go say there so the first example we did

say there so the first example we did

say there so the first example we did was go say hey and then we just said say

was go say hey and then we just said say

was go say hey and then we just said say there this time what I want us to do is

there this time what I want us to do is

there this time what I want us to do is to do say hey and then do the go say

to do say hey and then do the go say

to do say hey and then do the go say there so what’s gonna happen here is it

there so what’s gonna happen here is it

there so what’s gonna happen here is it just says hey hey hey right and so and

just says hey hey hey right and so and

just says hey hey hey right and so and if we made again both of these go

if we made again both of these go

if we made again both of these go routines whoops if we made them both go

routines whoops if we made them both go

routines whoops if we made them both go routines where is we get nothing so

routines where is we get nothing so

routines where is we get nothing so what’s going on here

what’s going on here

what’s going on here well the go routine is just kind of

well the go routine is just kind of

well the go routine is just kind of launched out but it’s gonna it’s running

launched out but it’s gonna it’s running

launched out but it’s gonna it’s running concurrently there’s nothing that that

concurrently there’s nothing that that

concurrently there’s nothing that that says hey this absolutely must finish

says hey this absolutely must finish

says hey this absolutely must finish right so if your program finishes before

right so if your program finishes before

right so if your program finishes before the go routine finishes it’s over it’s

the go routine finishes it’s over it’s

the go routine finishes it’s over it’s it’s non-blocking so it’s just not gonna

it’s non-blocking so it’s just not gonna

it’s non-blocking so it’s just not gonna do anything so even though we we asked

do anything so even though we we asked

do anything so even though we we asked these things to run they’re just not

these things to run they’re just not

these things to run they’re just not getting done that’s why when we said go

getting done that’s why when we said go

getting done that’s why when we said go say hey and then say there well this

say hey and then say there well this

say hey and then say there well this requires required to finish so it was

requires required to finish so it was

requires required to finish so it was definitely gonna finish and that’s why

definitely gonna finish and that’s why

definitely gonna finish and that’s why this was able to actually run and then I

this was able to actually run and then I

this was able to actually run and then I guess just the launching of making this

guess just the launching of making this

guess just the launching of making this concurrent took a little bit like longer

concurrent took a little bit like longer

concurrent took a little bit like longer and then we actually got to this point

and then we actually got to this point

and then we actually got to this point and then that’s why pride there is

and then that’s why pride there is

and then that’s why pride there is coming first not positive that’s just my

coming first not positive that’s just my

coming first not positive that’s just my guess so instead let’s say you said go

guess so instead let’s say you said go

guess so instead let’s say you said go say hey go say there we can kind of

say hey go say there we can kind of

say hey go say there we can kind of illustrate that that’s the only issue

illustrate that that’s the only issue

illustrate that that’s the only issue that we’re having by doing a time not

that we’re having by doing a time not

that we’re having by doing a time not sleep time diet let’s just do a second

sleep time diet let’s just do a second

sleep time diet let’s just do a second save that come up here and we can run

save that come up here and we can run

save that come up here and we can run this and sure enough everything runs it

this and sure enough everything runs it

this and sure enough everything runs it waits for a second in fact it just waits

waits for a second in fact it just waits

waits for a second in fact it just waits for a second right because these are

for a second right because these are

for a second right because these are just go routines so these launched

just go routines so these launched

just go routines so these launched launched it’s not actually waiting for

launched it’s not actually waiting for

launched it’s not actually waiting for these and then it sleeps for a second

these and then it sleeps for a second

these and then it sleeps for a second before the program ends so anyway so

before the program ends so anyway so

before the program ends so anyway so those are that’s just a really quick

those are that’s just a really quick

those are that’s just a really quick basic example of go routines but of

basic example of go routines but of

basic example of go routines but of course immediately you’re probably

course immediately you’re probably

course immediately you’re probably wondering well I mean that’s all fine

wondering well I mean that’s all fine

wondering well I mean that’s all fine and dandy and like a simple example

and dandy and like a simple example

and dandy and like a simple example where you just kind of add a sleep at

where you just kind of add a sleep at

where you just kind of add a sleep at the end and that’s all you got to do but

the end and that’s all you got to do but

the end and that’s all you got to do but in practice this is not a good idea

in practice this is not a good idea

in practice this is not a good idea right like you don’t want to

right like you don’t want to

right like you don’t want to handle for your go routines bye-bye

handle for your go routines bye-bye

handle for your go routines bye-bye thinking to yourself okay how long about

thinking to yourself okay how long about

thinking to yourself okay how long about should these take and then you know

should these take and then you know

should these take and then you know maybe I’ll just add a little more time

maybe I’ll just add a little more time

maybe I’ll just add a little more time and then that’s fine well cuz you’re

and then that’s fine well cuz you’re

and then that’s fine well cuz you’re gonna leave time on the table so surely

gonna leave time on the table so surely

gonna leave time on the table so surely there’s a better way right and there is

there’s a better way right and there is

there’s a better way right and there is and so in the next tutorial what we’re

and so in the next tutorial what we’re

and so in the next tutorial what we’re really talking about is how you can

really talking about is how you can

really talking about is how you can actually synchronize these go routines

actually synchronize these go routines

actually synchronize these go routines to wait for them to complete and to

to wait for them to complete and to

to wait for them to complete and to basically basically wait for them to

basically basically wait for them to

basically basically wait for them to complete without leaving time on the

complete without leaving time on the

complete without leaving time on the table using something arbitrary and

table using something arbitrary and

table using something arbitrary and hacky like time not sleep for some

hacky like time not sleep for some

hacky like time not sleep for some amount of time so anyways that’s what

amount of time so anyways that’s what

amount of time so anyways that’s what we’re gonna be dealing with in the next

we’re gonna be dealing with in the next

we’re gonna be dealing with in the next tutorial if you have questions comments

tutorial if you have questions comments

tutorial if you have questions comments concerns whatever feel free to leave

concerns whatever feel free to leave

concerns whatever feel free to leave them below otherwise I will see you in

them below otherwise I will see you in

them below otherwise I will see you in the next tutorial

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *