
- 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
Launching parallel tasks in Python
If a Python program can be broken into subprograms who is processing do not depend on each other, then each of the subprogram can be run in parallel when the overall program is being run. This concept is known as parallel processing in Python.
With multiprocessing
This module can be used to create many child processes of a main process which can run in parallel. In the below program we initialize a process and then use the run method to run the multiple sub-processes. We can see different sub processes in the print statement by using the process id. We also use the sleep method to see print the statements with a small delay one after another.
Example
import multiprocessing import time class Process(multiprocessing.Process): def __init__(self, id): super(Process, self).__init__() self.id = id def run(self): time.sleep(1) print("Running process id: {}".format(self.id)) if __name__ == '__main__': p = Process("a") p.start() p.join() p = Process("b") p.start() p.join() p = Process("c") p.start() p.join()
Output
Running the above code gives us the following result −
Running process id: a Running process id: b Running process id: c
- Related Articles
- Tasks in C#
- Program to find minimum time required to complete tasks with k time gap between same type tasks in Python
- Parallel Courses in Python
- Program to find maximum time to finish K tasks in Python
- Program to find minimum time to complete all tasks in python
- Launching the App Store from an iOS application
- Program to schedule tasks to take smallest amount of time in Python
- Program to find in which interval how many tasks are worked on in Python
- Program to find number of tasks can be finished with given conditions in Python
- Suspend/Resume tasks in FreeRTOS using Arduino
- What are the tasks in data preprocessing?
- Drawing multiple figures in parallel in Python with Matplotlib
- Program to check all tasks can be executed using given server cores or not in Python
- How to retrieve tasks in Task scheduler using PowerShell?
- How to schedule background tasks (jobs) in ASP.NET Core?

Advertisements