#golang #striversity 08.02 - 'goroutine' Context

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello good day and welcome back so today we're gonna look a little bit more closely at girl routine context and don't worry with a context word thing right now over time you'll have a better feel for what context is you can find a number of different definition for context and then a pan in the air quotes which you can see me doing of context of computer programming it has a certain type of meaning but different frameworks and I'll say I'll give you a context or whatever so our libraries and stuff so we're not gonna really try and define it we're gonna try and work with it and get a feel for what it is I know that sounds very weird but just trust me and we're gonna talk about what a goroutine does and in that way we're really gonna be looking at is what it's responsible for I notice that's different than what a goroutine is I'm not gonna try and tell you what a goroutine is just yet because that's a conversation that's gonna take us down sort of like a rabbit hole because we'll say o guru teams are lightweight tread but then we will have to talk about what a tread is how that differ from like you know the West red and we'll take about processes and how you know trips running are used to run your process and you know you could have multiple trips per process and you know of course most people trade for multiple processes and other side stuff so we're not gonna go down that road just yet hopefully by the end of this you'll kind of get a feel for that and maybe at a very high level we'll be able to come back and say okay know that you understand this know we can say well you can have multiple transport process or blah blah blah blah blah right but we'll ignore other and we're just gonna say we record stick to what is responsible for we're gonna talk about witness your program in we kind of know we're a program in when the main function because that's where your program starts in Maine where your main function in right and one of the things you're gonna mention here and later on is that how many goroutine can you have in one program a whole lot and again if you know anything about threads if you create too many chairs more than you have cores CPU cores or process eventually doesn't give you the benefit just because my engine the tread takes more overhead than you'd get from actually being able to run multiple treads so but in goroutine you could pretty much and you probably will run into that eventually too but even on a single core machine you can have hundreds of goroutine and that's because a whole routines are created versus hoe treads are created all right again we're not gonna get into the details of core routine versus tread right no we attack stick to what goroutine do for you so here's something important that I want to stand note and I want you to keep in the back image what happened when memes and your program ends when min ends are complete we know that already that once you problem starts at me and once made you reach an end of Main and that return that's when your program is this means the important thing here is this since your program ends when main end it means no matter how many go routines you create a million two zillion once mean and all of them gets terminated kills or whatever however you want to call it terminated killed be gentle be harsh they all end or the same also get cleaned up ok this is really really important and I'm a trade to demonstrate this later on in the video all right so in the first video I kind of showed you and I can say ok let's look at our chapter 7 section 3 or for example where we had a producer a consumer and try to make that the producer at least a goroutine and have it produce stuff and we'll eventually get to see how that's done so here let's start off with something very simple so here is a simple program and this program will can run I can assure you of that and if I leave this and I go here and I look at my cat man 0 I'm cat man I can do it again to share that and change it I say go run main you'll see it runs and I exit immediately it doesn't do anything so this is a valid program ok should notice by now when you invoke the your program to be run by the operating system I'm a skip over a bunch of detail eventually the go runtime is going creates a goroutine and I'm gonna number it and I'm calling a goroutine one and goroutine is responsible for running your main function and it does this by creating an execution context again let's don't focus on what I mean in the word context is I'm ultimately by just using it over and over and you'll get an idea and so basically it's an environment it's a logical construct but you know context is actually gonna be a set of values and so on it maintains to be able to manage the execution of the cout function and any other function that go mean invoke directly you think of it as a way of keeping track of where you are in the goal in the in your program as you traverse through main okay and so this is one goal routine that's gonna be created and it's gonna say hey go create a context and run my main function and when main and your program is now let's just say that oh we introduced another function and it doesn't matter the detail of this function it could be empty function we could do a ton of stuff it doesn't really matter and for me I call this function notice I just involved a function but it's a call I say call because if I use a assembly language or something I actually have something like say you know call routine or something like that okay and so I invoke this function because we say call this function producer and what happened is the go routine one is still I misspelled routine up there I'm gonna go fix that but I'll fix that in the slides let's go back up and make sure that item is right here geez I misspelled it everywhere so all right let me okay routine routine is is it go and I didn't notice this all this video all these videos I already started I created I did not know this is until I was going through it which means I have to get all these other slides here oh my gosh alright and I don't want to really start over right now so I'm gonna fix it right now and keep going so let's fix it that's that and don't try to ignore what you seen on the screen now until I explain it okay and that's what I think that's one and the last one okay now you know I'm not having any help doing these videos because if I have some help they're most likely a cut this okay so here we go so I have this call to the functional producer and so as you can see I still have this one goroutine it's gonna be called to manage set up a context to manage main invoke main but become main college function producer my goal routine is if for whatever reason I need to the processor need to be paused in the middle of producer that GU routine because it's maintainer context is gonna say oh I know that domain called producer and I'm part and silent producer somewhere and then when it's time to run again it would continue resume from there and continue on orbit after producer exits or finish it has to return back to me okay so that's where the context information is tracked so that it knows that oh hey I finished producer I returned back to main but it's still one goal routine one go routine that's why when we went into producer and we produced some values on a channel or a channel had to have buffer because producer here needed to stick some value into that channel and if it was blocked trying to stick some value no you see whether the go runtime would say hey the one and only route goroutine i have for this application this process is black so all process go routine that deadlock and it terminates because if it allows that condition you would never be able to resume that there's nothing else to be able to read from that shut up does that make sense okay so next example I create another function call consumer so I call my function producer then I call my function consumer notice what's happening where producer finishes like we said it returned to me hence why make and now consumer okay I still I have one goal routine that goes on run through producer when producer finish it comes back into me in Seattle means has called consumer it goes and run consumer and come back okay but I still only have one goal routine and that explains why when I'm inside of producer I do whatever producers has to do and that's it I can be doing anything else okay my way when I returned and I could do the work instead of computer Sumer and return and of course my program ends alright next example so so far we've seen that the responsibility of a goroutine is to run a function right create a context or a function and run it that's what I said before up here right she once you see our go routine is to manage the execution context of a function and that's what it's doing now when I stick the keyword go in front of producer notice the difference between these two the only difference here is this doesn't have the keyword go in front of producer here it does what happens is my goroutine one is treated and creates a context around main it's running main and admin says oh I want to interview to invoke the go statement which is some black box for us it's basically provided by the go runtime the go library I'll decide to start the go language so it's a black box first we don't see what happened in there but magic happens in there and what it does is it creates a second go routine let's go a go routine - it creates that go routine and say hey go routine - I want you to manage create a context and manage the function the execution function producer from trip ok and it returns back to main and then in main it says oh I gotta go around the function consumer but notice who's running the function in the consumer function it's my goal routine one go routine to is off manage the execution of unmentioned context for producer function for the producer function I know we can see we have to go routine sort of running concurrently I really try to not use the word parallel right concurrently and we'll see why because if I have one processor is bunks in between the two so I could imagine like system one CPU and one process it comes and it runs here and it gets into here it creates this function and just before it started running the first line of this function it stuck and says oh it's time to go run go routine one again and go retune one well that I can return so it goes and it runs start when a consumer I got some in the middle of consumer and said oh you know what it's time to go back to go routine - that's a goroutine - was like I was in the middle of this producer so I'm a continue from there blah blah bong-soon between them a lot of words they're not gonna make sense if there's a first time you sort of hearing this sort of thing but hopefully I'm saying no remember what I said way back here when does your program in it ends when your main program your main function ends ok so that means that once my function returns here the end of main this go routine to also goes away hence why I try to illustrate that by making sure that the arrow for this goal team does not go past the end of main here now once me again Byam everything gets cleaned up all right let's keep going down let's say I put go in front of producer and go in front of consumer simply a habit a goroutine one comes in running my main program blackbox creates a go routine to to run producer returns the main main creates a black box goes in there creates another go routine 3 and that runs consumer and then it returns and mins terminates and all to this routine also get Thurman Terminator right get to kill that now as it is even if I put some code inside of consumer producers to some work for them to do because the fact that though these go routine gets launched and again they can run by themselves and I return to main and immediately afterward I exit it doesn't matter what these guys are trying to do it wouldn't be enough time for them to get anything done and they're gonna cleaned up so there's a good chance you're not gonna see anything happen and I'll demonstrate that in a minute and that also come back to this idea when does my program in when my main function and and so when main ends doesn't matter what these other guys got launched to do they're gonna get cleaned up now finally this know should help you understand why is it when this is a much nicer set up for work ensue producers on consumer because we have a channel and here I took away the whole black box the Google stuff cuz we know it's gonna create go routines and we can see that if we have a channel and we tell our producers hey go produce on that channel that's gonna return back pretty much immediately then we tell a consumer cool consume from that channel know we have a go routine that run our producer Al Gore routine as runner consumer and the way they're communicating with each other which they're not really killing with each other but indirectly is the producers pushing value onto that channel and the consumers pulling value off of that channel okay and with that it doesn't matter if my channel is preferred on that because if it's buffered and producer produces so fast that it falls up eventually is gonna try and push a value on there and he's gonna be blocked and so all that happens is goroutine cannot run but this one gets blocked but that's okay they have two others that couldn't run and so here this guy is running and heats that consuming value and make space available and at some point or run time ago as the boron-10 go oh well no you can put values of a color channel because a space and even if this channel is and doesn't have a buffer well when he tries to put a value on there if the consumer is not ready to receive it he blocks but as soon as this consumer is ready to receive it he can run push that value on there and then the consumer now couldn't go read it similarly if the constitute right to read a value and the producer is not ready to put a value on there on a proper channel or channel that is empty well he this consumer would simply be black until the producers ready to put a value on them again in this setup if these two guys regard so what much work I got for them to do since I'm returning to main and exiting immediately they too will get cleaned up okay so let's look at some code so save that backup come back here and so we just run this code and to know we want to run the code of we're gonna say let's do a function call producer and we're gonna do a function call consumer and we're gonna call we're gonna say go producer and go consumer know in the example that I had on the slide I didn't have anything in there but let's do this print Lynn hello I am the producer and then we'll do something similar and say I'm the consumer and now with that save and then we go here and we go go around me and notice again you don't see anything on the output right are you thinking also because what I told you this launches those coroutine but then exit immediately to get to see those output we have to give the main some time so let's just do this let's do time that sleep and let's just sleep for maybe 200 100 milliseconds is that enough time let's see right and so we can have it sleep for a 100 milliseconds I don't know let's run it and see and it's enough time for these guys to print out the value and notice how consumer gets showed up before producer because once you create your routine you have no control the order in which they're gonna run now this group this consumer did not have to wait to this producer handsome as if to run first because there's nothing that said hey you have to wait for it now if on the other hand we're producing values and we say okay channel called equals make some channel of int and we say well we can make it on buffered one and you get this channel you get this channel but for the producer this is the output channel that on which you can say integers and then for the consumer it's an input channel on which you can only consume integers right so now we can see here you do for I equals to zero I less then you know ten or whatever twenty maybe I plus plus and then we're gonna say on the old channel I want you to send some value I times 2 and then after you finished reading I want you to close this channel out okay and for this guy we will say for be some value equals to range over its input channel now we know two when we use range over a channel so long as it's gonna close we're gonna eventually just terminate right and so I can say fmt that println you know V for example and I'm gonna sleep for 100 millisecond and we will see how many values I should be able to print up my are my twenty values in that time and you know there we go okay so because this get to I less than 20 so that's 19 - 19 times to study it's like those are my values I know we don't have to close before we have to close because we wanted to use range and bouncing but here what's gonna happen is my my for loop here my consumer is gonna block on this channel because it's not closed because waiting for this kite potentially to write some more values but guess what eventually my main will end and terminate my pro work so it is not a problem I know we don't even get to see that it's it's blocking and waiting for that we'll have to do something like a select right you have to do like for keep doing stuff select try to read from this channel case try to read from in write and then print it out and then if I can read I do the fault we can purge value from producer right so if we have that know you'll see I'm sitting there for a look forever and I'm trying to select I will try and read from I'm a trying to read from this input and then if I care if I read like a printout if I can I'll filter this message saying that oh hey the fault that I ain't go wrong and keep looking and of course eventually my program will oh well sir cannot refer to on exported FM t where is that having teeth for a capital P come on so right and you can see finish putting on my values ever since and was just waiting let's reduce this from one hundred milliseconds to maybe ten and let's see still a lot one millisecond still alive but you get the idea instead you know I could prove to you that all those values got printed out ahead the port and so hello I'm the producer you know and that's what's happening is that here my consumer is waiting and then I start printing out values and then eventually the producers got running and it's n1 values 0 and then consumer again waited a little bit and then it got a value from the producer and then it waited a whole lot more and I got another value from the producer and as you can see that's what's going on right so hopefully you can see now how this is happening to the magic of our little illustration here again this is a illustration purposes only a lot of simplification here but hopefully this conveys the idea he take away go keywords or go statement created a goroutine which manages the execution of a function it creates a context for that function add a key take away your program hands when mains and big big big takeaways if you ever do anything with goroutine and you don't see the output from them chances are your main is now waiting long enough okay and you're presenting and clearing everything I'm gonna cut this off here don't want to make it too long I want to spend most of the time on diagrams and pictures so you can understand what we're doing so far okay see in the next video I hope you're enjoying this I hope you're still excited spread the word subscribe if you haven't subscribed take care thanks again for your time very very much appreciate you spending time with me bye
Info
Channel: Vilito Exquisitus
Views: 1,021
Rating: undefined out of 5
Keywords: go, golang, go language, programming, tutorial, web development, training, computer programming, programming language, verrol, xiliax
Id: UaP-k6_ZaBc
Channel Id: undefined
Length: 22min 57sec (1377 seconds)
Published: Mon Mar 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.