Python Multithreaded Programming
Running several threads is similar to running several different programs concurrently with the following benefits −
· Multiple threads within a process share the same data space with the main thread and can share information or communicate with each other more easily than if they were separate processes.
· Threads sometimes called light-weight processes and they do not require much memory overhead; they are cheaper than processes.
A thread has a beginning, an execution sequence, and a conclusion. It has an instruction pointer that keeps track of where its context it is currently running.
· It can be pre-empted (interrupted)
· It can temporarily on hold while other threads are running is called yielding.
Starting a New Thread
To spawn another thread, need to call the method available in thread module:
This method call enables a fast and efficient way to create new threads in both Linux and Windows.
It is very effective for low-level threading, but the thread module is very limited compared to the newer threading module.
The Threading Module
The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads.
It exposes all the methods of the thread module and provides some additional methods
· threading.activeCount(): Returns the number of thread objects that are active.
· threading.currentThread(): Returns the number of thread objects in the caller's thread control.
· threading.enumerate(): Returns a list of all thread objects that are currently active.
In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows:
· run(): The run() method is the entry point for a thread.
· start(): The start() method starts a thread by calling the run method.
· join([time]): The join() waits for threads to terminate.
· isAlive(): The isAlive() method checks whether a thread is still executing.
· getName(): The getName() method returns the name of a thread.
· setName(): The setName() method sets the name of a thread.
Creating Thread Using Threading Module
To implement a new thread using the threading module to do the following −
· Define a new subclass of the Thread class.
· Override the __init__(self [,args]) method to add additional arguments.
· Then, override the run(self [,args]) method to implement what the thread should do when started.
The threading module provided with Python includes a simple-to-implement locking mechanism that allows to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock.
acquire(blocking) - Used to force threads to run synchronously enables to control whether the thread waits to acquire the lock.
release() - Used to release the lock when it is no longer required.
Multithreaded Priority Queue
The Queue module allows to create a new queue object that can hold a specific number of items. There are following methods to control the Queue −
· get(): The get() removes and returns an item from the queue.
· put(): The put adds item to a queue.
· qsize() : The qsize() returns the number of items that are currently in the queue.
· empty(): The empty( ) returns True if queue is empty; otherwise, False.
· full(): the full() returns True if queue is full; otherwise, False.