
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements