By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Python programming language is a multi-threading language. It means this language is capable of executing multiple program threads at a time or concurrently. A single Thread is a light weight process that performs a particular task during its lifecycle until it is terminated after that task completion.
Multithreading approach of programming has the following benefits. - A process may have multiple threads which share the same data space within the main thread. Therefore, they can communicate with each other and can share required information which is easier with less performance overhead as compared to separate processes. - As threads are light-weight processed therefore, they do not require much memory overhead. In terms of memory and performance, the threads are cheaper than processes. Each thread has a life cycle as the start, the execution and the termination. Each thread has an instruction pointer that keeps track of its context where it is currently running.
During the life cycle of a thread, the following events can also occur. - A Thread can be pre-empted or interrupted. - A Thread can be put on hold temporarily or sleep while other threads are executing or running. This is also known as yielding.
Starting a New Thread using “thread” module Python’s “thread” module has the method available that starts a new thread.
Following is the syntax to start a new Thread in Python programming language.
thread.start_new_thread ( function, args[, kwargs] )
Above method is used to create a new thread in both Linux and Windows operating systems. This method call returns instantly and the child thread starts to call the function that is passed in the list of arguments (args). When the called function returns, the thread will be terminated. In the above syntax, the args is a tuple of arguments. If we want to call function without passing any arguments, then we may pass an empty tuple as args. The parameter kwargs is an optional dictionary of keyword arguments. The Threading Module It provides much more powerful, high-level support for threads than the “thread” module discussed before. The “threading” module exposes all the methods that are present in the “thread” module and provides some additional methods as follows.
Method threading.activeCount (): This method returns the number of thread objects that are active. Method threading.currentThread (): This method returns the number of thread objects in the caller's thread control. Method threading.enumerate (): This method returns a list of all thread objects that are currently active.
In addition to these methods, the threading module has the Thread class that implements threading. Following are the methods provided by the Thread class. Method run (): The run () method of the Thread class is the entry point for a thread. Method start (): The start () method of the Thread class starts a thread by calling the run method. Method join ([time]): The join () method of the Thread class waits for threads to terminate. Method isAlive (): The isAlive () method of the Thread class checks whether a thread is still executing. Method getName (): The getName () method of the Thread class returns the name of a thread. Method setName (): The setName () method of the Thread class sets the name of a thread.
Creating Thread Using Threading Module Following are the steps to implement a new thread using the threading module. - Firstly, define a new subclass of the Thread class. - After inheritance, Override the __init__ (self [, args]) method to add additional arguments. - Next, override the run (self [, args]) method to implement what the thread should do when started.
After doing above steps, we can now create the instance of subclass and then start a new thread by invoking the start () method, which in turn will call the run () method.
Following is the Thread example by using “threading” threading module in Python language:
Output When we execute the above Python program, we will observe the following output. Synchronizing Threads in Python The simple-to-implement locking mechanism is provided in the “threading” module of Python that permits us to synchronize threads.
It has following methods to achieve Thread synchronization. - Method Lock (): When this method is called it returns the new lock. - Method acquire (blocking): This method of the new lock object is used to force threads to run synchronously. It accepts an optional blocking parameter that enables us to control whether the thread waits to acquire the lock. If the value of blocking is set to 0 then the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If the value of blocking is set to 1 then the thread blocks and wait for the lock to be released. - Method release (): This method of the new lock object is used to release the lock when it is no longer required.
et’s understand thread synchronization with the help of following example. Output When we execute the above Python program, we will observe the following output.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.