Net-informations.com
SiteMap  | About    

Python Threading Basics

What is a Thread?

A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler , which is typically a part of the operating system. Thread in a computer program is an execution path. Threading is creating additional independent execution paths in your program. Every program starts with at least one execution path/thread. You can create more threads to execute parallel tasks depending on your requirements. It is a concept of efficient resource utilization . Having multiple threads in an application provides two very important potential advantages:

  1. Improve an application's perceived responsiveness.
  2. Improve an application's real-time performance on multicore systems.

There are libraries to achieve it. It is important to note that Threading requires careful synchronization to avoid race conditions.

How to create a Thread in Python

The "thread" module provides simple functionalities and higher level interface is provided within the threading module that should be used instead. First thing you need to do is to import Thread using the following code:

A simple Thread program

The following example shows how to run a function as Threads in Python. The simplest way is via the thread module and its start_new_thread() method.

The thread.start_new_thread start a new thread and return its identifier. When the function returns, the thread silently exits.

example

Creating a Thread Class

Python provides the threading module which implements a layer on top of the thread module . The threading module provides, among other things, a Thread class which contains a run() method . Typical usage is to subclass the Thread class and override the run() method in the subclass to implement the desired functionality. In order to create a thread in Python you'll want to make your class work as a thread. For this, you should subclass your class from the Thread class .

Here, MyThreadClass is a child class of the Thread class . Next step is to define a run method in this class. The run() method in the MyThreadClass is the entry point for a thread. The run() method will be executed when we call the start method of any object in our MyThreadClass class. You can write code inside the run() method for running thread activity. It is possible to pass a function or other callable object to the Thread class constructor to specify the target that the run() method will call. To do this, we can use the function thread.start_new_thread :

Full Source

output

In the above example, you can see the class ThreadClass inherits from threading.Thread and because of this, you need to define a run() method that executes the code you run inside of the thread. The only thing of importance to note in the run method is that self.getName() is a method that will identify the name of the thread. Finally, t.start() is what actually starts the threads.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.