How to do Multiprocessing in Python - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how to do multiprocessing in Python using an example.
It is a way to run multiple processes at the same time given that the machine has support for multiple processors. The main advantage is of multiprocessing is that the system actually performs multiple independently executable parts of an application at the same time which eventually increases the efficiency and performance of the system. Before moving further let’s see what is a process identifier.
A Process Identifier or PID, in short, is a  number that uniquely identifies each running process in an operating system which can be Unix,  Linux, macOS, and MS Windows.
Python provides an inbuilt package named multiprocessing which provide API that effectively supports concurrency, distributing the input data across processes or parallel executions of different processes. In the above section, we have learned about process identifier and the built-in os module of python provides us with a method named getpid() which returns the PID of the process.
Let’s create a function that takes an integer and return whether it is positive or not and print the PIDs.
import multiprocessing 
import os 

def isPositive(n): 
    print("isPositive function with Process ID:",os.getpid())
    if n>=0:
        print("Integer passed is Positive:")
    else:
        print("Integer passed is Negative:")

if __name__ == "__main__": 
    print("Main function with Process ID:",os.getpid()) 
    isPositive(-1)
    isPositive(2)
Output:
Main function has Process ID: 7644
isPositive function has Process ID: 7644
Integer passed is Negative:
isPositive function has Process ID: 7644
Integer passed is Positive:
Note: PID can be different from the above output.
From the output, we see that all the function calls that we have called has the same Process ID which is same as that of the main function.
Next, we will use the components of the multiprocessing module to run the tasks as different processes.
Whenever we want to create a new process we create an object of the Process class whose constructor takes the following arguments.
target – It is the callable object to be invoked by the run() method and default value is None.
args – It is the argument tuple that is passed in the function specified in the target.
name – It is the process name as a string. the default value is None.
There are some other methods provided by this class which are as follows.

start() – It will start the process’s activity and is called at most once for each process.
run() – This invokes function passed as the target argument.
join([timeout]) – This method blocks until the process whose join() method is called terminates.
is_alive() – Returns True if a process is alive else False.
pid – Return the process Id of a process

start() – It will start the process’s activity and is called at most once for each process.
run() – This invokes function passed as the target argument.
join([timeout]) – This method blocks until the process whose join() method is called terminates.
is_alive() – Returns True if a process is alive else False.
pid – Return the process Id of a process
We will try to perform multiprocessing on the above example using the Process class.
import multiprocessing 
import os 

def isPositive(n): 
    print("isPositive function with Process ID:",os.getpid())
    if n>=0:
        print("Integer passed is Positive")
    else:
        print("Integer passed is Negative")

if __name__ == "__main__": 
    print("Main function with Process ID:",os.getpid())
    proc1 = multiprocessing.Process(target=isPositive,args=(8,))
    proc2 = multiprocessing.Process(target=isPositive,args=(-6,))
    proc1.start() 
    proc2.start() 
    print("Process proc1 has Process ID:",proc1.pid) 
    print("Process proc2 has Process ID:",proc2.pid) 
    proc1.join() 
    proc2.join()
Output:
Main function with Process ID: 10124
Process proc1 has Process ID: 14912
Process proc2 has Process ID: 11708
isPositive function with Process ID: 14912
Integer passed is Positive
isPositive function with Process ID: 11708
Integer passed is Negative
From the output above, we can see that in total we have 3 processes running, one main process and two subprocesses of the main process.
So we learned about multiprocessing in python.
Official Documentation
Python Classes and Objects
Happy Learning 🙂

Different ways to use Lambdas in Python
How to pass Command line Arguments in Python
Python raw_input read input from keyboard
Python Decorators – Classes and Functions
How to Remove Spaces from String in Python
How to access for loop index in Python
What are Python default function parameters ?
How to sort python dictionary by key ?
Python – How to merge two dict in Python ?
Python TypeCasting for Different Types
How to remove empty lists from a Python List
Python How to read input from keyboard
How to Create or Delete Directories in Python ?
What are the different ways to Sort Objects in Python ?
How to use *args and **kwargs in Python

Different ways to use Lambdas in Python
How to pass Command line Arguments in Python
Python raw_input read input from keyboard
Python Decorators – Classes and Functions
How to Remove Spaces from String in Python
How to access for loop index in Python
What are Python default function parameters ?
How to sort python dictionary by key ?
Python – How to merge two dict in Python ?
Python TypeCasting for Different Types
How to remove empty lists from a Python List
Python How to read input from keyboard
How to Create or Delete Directories in Python ?
What are the different ways to Sort Objects in Python ?
How to use *args and **kwargs in Python
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list