The simplest way to use a thread is to instantiate it with a target function. This video shows us how to do that.
- [Narrator] Welcome to the second section of this video course, Thread-Based Parallelism. In the previous section we studied parallel computing in Python. In this section we'll examine the features offered by the threading library with different application examples. We'll start off by defining a thread, and then we'll determine it. We'll also see how to use a thread in subclass and further understand how to do the thread synchronization. Later we'll learn how to use the statement and carry out thread communication using queue. In the end, we'll evaluate the performance of multithread applications.
Welcome to the first video of this section, titled Defining a Thread. In this video we'll define a thread with the help of threading module. Further, we'll see how to instantiate a thread. Finally we'll pass an argument to the function. First, let's understand how to use the Python threading module . Python manages a thread via the threading package that is provided by the Python standard library. This module provides some very interesting features that make the threading-based approach a whole lot easier. In fact, the threading module provides several synchronization mechanisms that are very simple to implement.
The major components of the threading module are, one, the thread object. Two, the Lock object. Three, the RLock object. Four, the semaphore object. Five, the condition object. And six, the event object. Now let's define a thread. The simplest way to use a thread is to instantiate it with a target function and then call the start parentheses method to let it begin its work. The Python module threading has the Thread parentheses method that is used to run processes and functions in a different thread.
In the code which you can see on your screen there are keywords such as, one, group. This is the value of of group that should be none. This is reserved for future implementations. Two, target. This is the function that is to be executed when you start a thread activity. Three, name. This is the name of the thread. By default a unique name of the form thread N is assigned to it. Four, args. This is the tuple of arguments that are to be passed to target. Five, kwargs.
This is the dictionary of keyword arguments that are to be used for the target function. It's useful to spawn a thread and pass arguments to it that tell it what work to do. This example passes a number, which is the thread number, and then prints out the result. Let's see how to define a thread with the threading module. For this a few lines of code are necessary. To import the threading module we simply use the Python command as shown. In the main program we instantiate a thread using the thread object with a target function called function.
Also we pass an argument to the function that will be included in the output message. The thread doesn't start running until the start method is called. And that join method makes the calling thread wait until the thread has finished the execution. Let's run this code and see the output. Open your command prompt and navigate to your working directory. Then run the Python file as shown. The output of the code looks like this. We should also point out that the output could be achieved in a different manner. In fact multiple threads might print out the result back to STD out at the same time so the output order cannot be predetermined.
Great, so in this video we saw how to define a thread. Our next video shows us how to determine the current thread.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Memory organization
- Parallel programming models
- Designing a parallel program and evaluating performance
- Working with threads in Python
- Synchronizing threads and using multithreading
- Spawning a process
- Running a process in the background
- Synchronizing processes
- Using the mpi4py Python module
- Using collective communication
- Reducing operations
- Managing events, tasks, and routines with Asyncio
- Distributing tasks