From the course: Python Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Multithreading

Multithreading

- [Instructor] Processes and threads may seem like abstract concepts right now, but let's get hands-on and start spinning some up. The other programmers are going to look at your code and say, "Nice threads." The first thing we want to do is import the threading and time modules. So import threading, import time. Then we're going to create a function that calculates the square of a number but takes a really long time to do it. We can call it, appropriately, longSquare. You pass in a number time.sleep for a whole second and then return that number squared. Let's say we want to calculate the square of a few numbers. It's going to take a long time. For in and range zero through five. This is obviously a fairly contrived example, but these situations often arise in programming. For instance, waiting to fetch data back from a remote server. Your code is just sitting around doing nothing, waiting for that data to come back,…

Contents