Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

Threading Tutorial #1 - Concurrency, Threading and Parallelism Explained

Tech With Tim11:34 254.691 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 82 Zeilen
Herunterladen
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. have central processing units those central processing units are made up of course the amount of cores you have denotes the amount of parallel
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. own threads but threading does not involve running on multiple cores all it involves is creating some program or some operations some function something
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. 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
  78. 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
  79. 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
  80. 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
  81. 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
  82. threads and python in the next video

Zum Nachlesen