From the course: Programming Foundations: Beyond the Fundamentals
Introduction to multithreading - Python Tutorial
From the course: Programming Foundations: Beyond the Fundamentals
Introduction to multithreading
(knife chopping) - I love to cook. One of my favorite dinners is stir-fried vegetables and rice. I could chop the vegetables, cook them up, and then set them aside and put on the rice, but that's going to make the entire prep time longer. Especially when I'm hungry, that's not something I want. So instead, I put the rice on first. And while it's cooking, I prep and cook the vegetables. That way, everything cooks at the same time, and my meal is finished more quickly. This lets me do two things at once, but I still only need to focus my attention in one direction at any given moment. You can make your computer programs faster and more responsive by structuring them to do multiple things at once. To do this, you can structure your code to start separate tasks that are executed simultaneously. Each task is known as a thread, and the overall approach to writing code that executes threads concurrently is known as multi-threading. Each thread in a multi-threaded program requires additional processing power and memory from the computer where the code is being executed. A computer's resources are finite though, so it's important to balance the benefits of multi-threading with the potential performance impacts on the computer as a whole. Consuming too much processor power or computer memory can slow the entire computer system, which cancels out any increase in responsiveness from splitting code into multiple threads. Many languages including Python support multi-threading. You may hear the term asynchronous code in relation to other languages like JavaScript. It's not exactly the same as multi-threading, but it's very similar. When you're building your programming skills by writing basic programs, there's no need to worry about incorporating multi-threading. But once you've mastered the basics and you're creating more complex applications, come back to the library to learn about multi-threading in your chosen language and see what it can do for you.