- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Thread-based parallelism in Python
A thread in computer science is a set of instructions that can be managed independently by a scheduler, which is a part of operating system.
The main function of Threading is to run multiple threads at a time. Threads means different tasks, function calls in the program and multiple threads run at the same time that does not means that they are executed on different machines.
Multi-threading is used in two cases.
When the outputs of the sub-programs need to combined with main program.
When main program contains piece of code that are relatively independent of each other.
Python provides Threading module which is very powerful and also provides high level support for threads.
Threading module defines lots of functions which are used to obtain thread related data and these functions are executed automatically.
This function returns the number of Thread objects currently alive. Here the returned count is equal to the length of the list returned by enumerate().
This function returns the current Thread object and it is corresponding to the caller’s thread of control.
This function returns the ‘thread identifier’ of the current thread. This is a nonzero integer.
This function returns a list of all Thread objects currently alive includes daemonic threads, current_thread() function creates a dummy thread and the main thread and excludes terminated threads and threads that have not yet been started.
This function returns the main Thread object.
When all threads are started from the threading module, set a trace function. Before run() method is called, this function is passed to sys.settrace() for each thread.
When all threads are started from the threading module, set a profile function. Before run() method is called, this function is passed to sys.setprofile() for each thread.
This function returns the size of the thread stack and it is used when creating new threads.
This is a constant which has the maximum value allowed for the timeout parameter of blocking functions(Lock.acquire(), RLock.acquire(), Condition.wait(), etc.).
import threading def trace_function(): print("Passing the trace function") def profile(): print("PROFILE THREAD: " + str(threading.current_thread().getName())) class mythread(threading.Thread): def __init__(self, thread_name, thread_ID): threading.Thread.__init__(self) self.thread_name = thread_name self.thread_ID = thread_ID def run(self): print(str(self.thread_ID)); print("ACTIVE THREADS ARE: "+ str(threading.active_count())) print("CURRENT THREAD IS: " + str(threading.current_thread().getName())) my_thread1 = mythread("PP", 500) my_thread2 = mythread("PythonProgram", 1000); print("NAME OF THE MAIN THREAD: " + str(threading.main_thread().getName())) print("IDENTIFICATION OF MAIN THREAD: "+ str(threading.get_ident())) print("STACK SIZE = " + str(threading.stack_size())) print(threading.settrace(trace_function())) threading.setprofile(profile()) my_thread1.start() my_thread2.start() print("LIST OF ENUMERATION: ") print(threading.enumerate()) print("EXIT")
NAME OF THE MAIN THREAD: MainThread IDENTIFICATION OF MAIN THREAD: 5436 STACK SIZE = 0 Passing the trace function None PROFILE THREAD: MainThread 500 1000LIST OF ENUMERATION: ACTIVE THREADS ARE: 6 [<_MainThread(MainThread, started 5436)>, <Thread(Thread-4, started daemon 1960)>, <Heartbeat(Thread-5, started daemon 6452)>, <HistorySavingThread(IPythonHistorySavingThread, started 4304)>, <mythread(Thread-8, started 8460)>, <mythread(Thread-9, started 4668)>] EXIT CURRENT THREAD IS: Thread-8 ACTIVE THREADS ARE: 5 CURRENT THREAD IS: Thread-9
- Related Articles
- Thread-based parallelism in C#
- Data parallelism vs Task parallelism
- Starting a New Thread in Python
- Types of Parallelism in Processing Execution
- How to catch a thread's exception in the caller thread in Python?
- How to use Thread in Tkinter Python?
- Difference between Concurrency and Parallelism
- Main thread vs child thread in C#
- User Thread vs Daemon Thread in Java?
- What are the types of Parallelism in Computer Architecture?
- What are the conditions of Parallelism in Computer Architecture?
- What are the different levels of Parallelism?
- What are different methods of parallelism in Parallel Computer Architecture?
- Python Image based Steganography