A multi-threaded program consists of sub-programs each of which is handled separately by different threads. Multi-threading allows for parallelism in program execution. All the active threads run concurrently, sharing the CPU resources effectively and thereby, making the program execution faster. Multi-threading is generally used when:
- There are sub-programs whose output needs to be combined by the main program.
- The main program contains sections of code that are relatively independent of each other.
A multi-threaded program handles different tasks at the same time, within the same process, where different threads share the data space with each other as well as the main thread.
Starting a new thread
The threading module in python provides function calls that is used to create new threads. The __init__ function is used for initializing the data associated with the new threads whereas, the run function defines the thread’s behavior as soon as the thread starts it’s execution.
In order to create a new thread, :
- Create a sub class of the thread class.
- Override the __init__ function of the thread class. This method will initialize the date specific to a thread.
- Override the run method to define the behavior of the thread.
GFG 1000 GeeksforGeeks 2000 Exit
Threading module in python provides powerful and high level support for threads.
Threading module defines the following function calls that is used to obtain thread related data. All these functions are executed atomically.
- active_count(): Return the number of Thread objects currently alive. The returned count is equal to the length of the list returned by enumerate().
- current_thread(): Return the current Thread object, corresponding to the caller’s thread of control. If the caller’s thread of control was not created through the threading module, a dummy thread object with limited functionality is returned.
- get_ident(): Return the ‘thread identifier’ of the current thread. This is a nonzero integer. Its value has no direct meaning; it is intended as a magic cookie to be used e.g. to index a dictionary of thread-specific data. Thread identifiers may be recycled when a thread exits and another thread is created.
- enumerate(): Return a list of all Thread objects currently alive. The list includes daemonic threads, dummy thread objects created by current_thread(), and the main thread. It excludes terminated threads and threads that have not yet been started.
- main_thread(): Return the main Thread object. In normal conditions, the main thread is the thread from which the Python interpreter was started.
- settrace(func): Set a trace function for all threads started from the threading module. The func will be passed to sys.settrace() for each thread, before its run() method is called.
- setprofile(func): Set a profile function for all threads started from the threading module. The func will be passed to sys.setprofile() for each thread, before its run() method is called.
- stack_size([size]): Return the thread stack size used when creating new threads.
This module also includes the constant:
- TIMEOUT_MAX: The maximum value allowed for the timeout parameter of blocking functions (Lock.acquire(), RLock.acquire(), Condition.wait(), etc.). Specifying a timeout greater than this value will raise an OverflowError.
Name of main thread: MainThread Identity of main thread: 139964150720320 Stack size = 0 Passing the trace function None Setting the profile of thread: MainThread 1000 Number of active threads: 2 Name of current thread: Thread-1 2000 Number of active threads: 2 Name of current thread: Thread-2 Enumeration list:  Exit
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.