The document discusses multithreaded programming in Python, highlighting the benefits of threads over processes, such as shared data space and lower memory overhead. It details how to create and manage threads using the threading module, including methods for synchronization and managing thread execution. Additionally, it briefly mentions the queue module for handling multithreaded queues and indicates a forthcoming post on GUI programming with Tkinter.
Running several threadsis similar to running several different programs concurrently, but with the following benefits Multiple threads within a process share the same data space with the main thread and can therefore 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. Python - Multithreaded Programming
3.
A thread hasa beginning, an execution sequence, and a conclusion. It has an instruction pointer that keeps track of where within its context it is currently running. It can be pre-empted (interrupted) It can temporarily be put on hold (also known as sleeping) while other threads are running - this is called yielding.
4.
To spawn anotherthread, you need to call following method available in thread module Starting a New Thread thread.start_new_thread ( function, args[, kwargs] ) This method call enables a fast and efficient way to create new threads in both Linux and Windows. The method call returns immediately and the child thread starts and calls function with the passed list of args. When function returns, the thread terminates.
5.
The Threading Module Thenewer threading module included with Python 2.4 provides much more powerful, high- level support for threads than the thread module discussed in the previous section. The threading module 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.
6.
In addition tothe 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.
7.
Creating Thread UsingThreading Module To implement a new thread using the threading module, you have 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. Once you have created the new Thread subclass, you can create an instance of it and then start a new thread by invoking the start(), which in turn calls run() method.
8.
Synchronizing Threads The threadingmodule provided with Python includes a simple-to-implement locking mechanism that allows you to synchronize threads. A new lock is created by calling the Lock() method, which returns the new lock. The acquire(blocking) method of the new lock object is used to force threads to run synchronously. The optional blocking parameter enables you to control whether the thread waits to acquire the lock.
9.
If blocking isset to 0, the thread returns immediately with a 0 value if the lock cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1, the thread blocks and wait for the lock to be released.
10.
Multithreaded Priority Queue TheQueue module allows you 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.
11.
Python - GUIProgramming (Tkinter) Statistic Probability Stay Tuned with Topics for next Post