Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).
Threading Tutorial #1 - Concurrency, Threading and Parallelism Explained
Das Wichtigste aus dem Video
Tipp auf eine Zeit – das Video springt genau dorthin.
Transkriptautomatisch erstellt · 82 Zeilen
- hello everybody and welcome back so in this video we're gonna be talking about threading so it's actually the introduction to this reading in Python
- tutorial series in this video I will warn you we're not actually gonna be doing any coding we're just gonna be talking about the theory so what is a
- thread how do we run threads what's the difference between threads and processes and really understanding why we even need to use these things so I'm just
- gonna warn you if you don't understand that please don't skip forward to the next video because there's no point in learning threading unless you understand
- what the heck it actually is so I'm gonna hop onto the whiteboard now and we'll get started and talk about what is a thread what is threading and why do we
- use it okay so what I'm drawn on the screen right now is kind of a classic processor in 2020 we have 4 cores which are all these red boxes here that I'm
- kind of drawing on and then the actual black box that's a note it by CPU is the entire central processing unit so back in the old days we used to have
- processors that just had one processing core now we have processors with 4 cores 8 cores 2 cores 16 cores 32 cores and it goes pretty crazy right so computing has
- expanded a ton now the reason I'm starting with this is because we actually need to understand how our processor works to understand what a
- thread is and why we have you know thousands of them and why we create them in our programs so the idea here is that the amount of cores you have on your
- processor is the amount of things that can happen at the exact same time yes that's what I mean if we're talking about actual time you know like theory
- of relativity whatever you want to put it out if we have four cores that means at any point in time we can do at most four operations at the exact same time
- and these operations are really low-level computational operations there so you know millions of them happening a second but in these tiny nano seconds
- that it takes to do an operation we can only do four of them at the same time because we have four cores and this is called parallelism if we have multiple
- things happening at the exact same time then that is a parallel operation right we can think about like if we have four roads and we have four cars driving on
- these roads right moving at the same time they're driving parallel to each other at the same time whereas if I have you know a bunch of
- cars beside each other on the same road or a bunch of cars behind each other they can only go as fast as the car in front of them right or they need to wait
- for the car in front of them to get off the road before they can start driving on it right and that's kind of the idea so we'll use some examples like this to
- make things more clear but just understand that the amount of cores in your processor really matters right and when we think about clock speed so if we
- have something like 2.6 gigahertz is the clock speed of your processor what that means is each one of your cores can run at 2.6 gigahertz so each one of those
- cores is able to do 2.6 I think it's like million operations per second or you know something along those lines I don't know what the conversion rate is
- and that means you can do 2.6 times 4 operations per second because each one of these cores is running at that clock speed and the clock speed is essentially
- kind of an electron or a wire going on and off on and off on and off and that allows your computer to do operations so that's kind of the background here we
- have central processing units those central processing units are made up of course the amount of cores you have denotes the amount of parallel
- operations that can happen at the same time now obviously we know that we're doing more than four operations right you know like in our computer we're
- gonna do more than four operations so how do we do all these operations and how do we schedule when they happen well that's where we talk about threads and
- we talk about multi processing so a thread is essentially one program or one set of operations that needs to happen so every thread is going to be assigned
- to one core so all of these cores that we have here will have a bunch of different threads that they're going to be executing and switching between which
- ones they perform operations on so if we say that like this line actually I'm gonna draw in another color so that it's a little bit better let's say this blue
- line is a thread and this other blue line is a thread and our processor are a processor core can only do one thing at a time and it has both these threads
- that it's assigned to what that means is that it needs to find a way where it can do some operations on this thread and can switch right and do some operations
- on this thread and this is what threading is it's essentially how do we determine when to run different things on the same CPU core yes we can have
- threads running on different cores obviously you know this core itself is gonna happen its own threads that are running on it this core will have its
- own threads but threading does not involve running on multiple cores all it involves is creating some program or some operations some function something
- that's going to be executing in a different sequence than another thing so if we have two threads you know then we can draw these two lines here let's say
- okay we have these two different things they both need to happen now we tell our CPU you know figure out which one we're gonna do and what's happening on our
- computer and we can actually look at the amount of threads and I'll do that I'm here to show you guys if we go to task manager and we go to performance we
- should see that we have 1854 threads running and you can see that the bottom and 173 different processes so what that means is that between the four cores on
- my computer you can see it's running a little bit under 4.2 gigahertz we can run at most four threads at once because we have four cores and all those other
- threads are just being switched between on the CPU cores so all these different operations are happening you know milliseconds after each other and this
- is the idea behind threading is that we're not necessarily doing things in parallel at the same time we're just changing the order in which we do
- specific operations so why would we even want to do that what's the point of making multiple threads if we can't do things at the same time well sometimes a
- thread does what's called hanging or it stops or he doesn't need to actually be executing at the current time so our processor core can kind of turn around
- and execute another thread well this one say is waiting right so while one threads waiting for something to happen maybe the user to press a key maybe for
- you know some network thing to send a file we don't need to be just stuck waiting for this thread our processor core can kind of chain gears it can
- shift to the left and it can execute another thread while that one's waiting so this is the point with threading and this is called concurrent programming
- not when we're doing things in parallel at the exact same time but when we're doing things in different timing sequences so we can
- have multiple threads running at the same time and our one CPU core that we're running this these threads on is switching between these threads in its
- execution chain right so I'm gonna erase this and we're gonna go down to a one core model now because this is kind of showing you how the entire CPU works as
- a whole right there's all these threads they get distributed between the cores and then they run on those cores these cores can only do one thing at a time
- there's four of them that means we can do four things at a time in total so these cores will switch what they're gonna be doing based on the threads that
- they have so let's do a one core model now so I'm gonna just draw one CPU core now and show you kind of the difference between what happens if we run something
- in one thread versus running it and say two or three and why we would even do that so let's say we just have a basic Python application and we want to print
- one we want to time dot sleep say 10 seconds right so we'll just put 10 seconds this is just pseudocode don't really worry about the syntax too much
- and then we want to print the value too now if we were running this in one thread so let's draw our CPU let's make this like orange red box this will be
- our our core so let's write this is a core and we put our 1 thread so we'll say this like here well let's just put you know maybe a label for this we'll
- say that's like t1 so thread 1 and this will be thread 1 so for us to run this program and to see the output of one and see the output of 2 this is gonna take
- us a little bit over 10 seconds right because we need to print 1 then we need to wait for 10 seconds and then we need to print 2 so we can imagine it's not
- gonna take much longer than 10 seconds but a tiny bit more it's gonna take longer than that right so for us to get that output takes 10 seconds now if I
- distribute this into 2 threads where what I do is in thread 1 I leave what I have there but in thread to so in t2 like this and we'll do a little bit
- better here well print the value of 2 if I do this and now I throw my t2 over here going in my core so both these threads are running on this same core
- which means again only one thing can happen at a time what we can do now is say ok so we'll print one cuz we're gonna start
- that thread one in our program but then when we hit this sleep so let's just say one's out put it here we don't actually need to wait for this entire sleep to
- happen what we can do is we can say okay so since this thread isn't doing anything right now the core doesn't have any operations to perform we're not
- adding we're not subtracting we're just merely waiting I don't need to just stall and hang on this thread in fact what I can do is almost think of it like
- rotate the core over or you know pass another thread to the core we can go over to thread two and just print the value two immediately because the thing
- is if thread one is sleeping we don't need to wait for it we can just go to another thread do something there and then once this thread finishes executing
- or finishes sleeping or waiting we can go back to it and we can execute the rest say so we had like print three under here the output from our program
- with these two threads would be one we would sleep so we would go to thread two we finish thread two we'd go back to thread one we'd wait for it to finish
- sleeping and then we'd output the value three and this is the idea right and threads are really useful in web applications or when you're doing like
- online games and stuff because you don't want to pause the entire screen while you wait to receive like a few megabytes of data from the server in fact what you
- want to do is have all of the things that are like server related commands running in a thread so that while you're waiting for the server to return a
- response to you your whole game doesn't just freeze you can have these threads so that it is going in between these two different threads so you know one thread
- is handling getting the messages and stuff from the server and the other thread is handling actually displaying the graphics to you as a user so you're
- playing a video game right so then as soon as the thread from the servers are ready to receive and ready to start working okay it'll switch to that it'll
- get the information and then you can translate that to the graphics thread which will start out what do you call it displaying to the screen or like
- updating the screen right so that's the idea with these threads now a lot of applications are multi processed which means a little bit more complicated to
- do they have their threads running on different CPU cores at the same time but we're not gonna get into that for this tutorial because that's a little bit
- more complicated advanced just understand that if we're waiting for something to happen in one thread we will switch to the other thread or the
- other threads because there may be many execute those as much as we can and then go back to the original thread when it stops waiting so that's the idea that's
- my explanation of threads and processes and I hope with that you guys have a good understanding and are ready to move on to programming and getting into
- threads and python in the next video
Zum Nachlesen
MultithreadingMultithreading (englisch wörtlich für Mehrfädigkeit oder auch Mehrsträngigkeit und, weiter übertragen, die Nebenläufigkeit) bezeichnet in der Informatik das …
Thread (Informatik)Kritischer Abschnitt · Nebenläufigkeit · Parallele Programmierung · Prozess · Threadsicherheit. Literatur. Bearbeiten. Peter Ziesche: Nebenläufige & verteilte …
Parallele ProgrammierungEs umfasst zum einen Methoden, ein Computerprogramm in einzelne Teilstücke aufzuteilen, die nebenläufig ausgeführt werden können, zum anderen Methoden, …
MultitaskingDer Begriff Multitasking [ˌmʌltiˈtɑːskɪŋ] (engl.) bzw. Mehrprozessbetrieb bezeichnet die Fähigkeit eines Betriebssystems, mehrere Aufgaben (Tasks) …