Python Tutorial

Introduction Python Features Python Applications System requirements for Python Python Installation Python Examples Python Basics Python Indentation Python Variables Python Data Types Python IDE Python Keywords Python Operators Python Comments Python Pass Statement

Python Conditional Statements

Python if Statement Python elif Statement Python If-else statement Python Switch Case

Python Loops

Python for loop Python while loop Python Break Statement Python Continue Statement Python Goto Statement

Python Arrays

Python Array Python Matrix

Python Strings

Python Strings Python Regex

Python Built-in Data Structure

Python Lists Python Tuples Python Lists vs Tuples Python Dictionary Python Sets

Python Functions

Python Function Python min() function Python max() function Python User-define Functions Python Built-in Functions Python Recursion Anonymous/Lambda Function in Python python apply() Function Python lambda() Function

Python File Handling

Python File Handling Python Read CSV Python Write CSV Python Read Excel Python Write Excel Python Read Text File Python Write Text File Read JSON File in Python

Python Exception Handling

Python Exception Handling Python Errors and exceptions Python Assert

Python OOPs Concept

OOPs Concepts in Python Classes & Objects in Python Inheritance in Python Polymorphism in Python Python Encapsulation Python Constructor Python Super function Python Static Method Static Variables in Python Abstraction in Python

Python Iterators

Iterators in Python Yield Statement In Python

Python Generators

Python Generator

Python Decorators

Python Decorator

Python Functions and Methods

Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods

Python Modules

Python Modules Python Datetime Module Python Math Module Python Import Module Python Time Module Python Random Module Python Calendar Module CSV Module in Python Python Subprocess Module Python Subprocess

Python MySQL

Python MySQL Python MySQL Client Update Operation Delete Operation Database Connection Creating new Database using Python MySQL Creating Tables Performing Transactions

Python MongoDB

Python MongoDB

Python SQLite

Python SQLite

Python Data Structure Implementation

Python Stack Python Queue Python Linked List Python Hash Table Python Graph

Python Advance Topics

Speech Recognition in Python Face Recognition in Python Python Linear regression Python Rest API Python Command Line Arguments Python JSON Python Virtual Environment Type Casting in Python Python Collections Python Commands Python Data Visualization Python Debugger Python DefaultDict Python Enumerate

Python 3

Anaconda in Python 3 Anaconda python 3 installation for windows 10 List Comprehension in Python3

Misc

Python PPTX Python Pickle Python Seaborn Python Coroutine Python EOL Python Infinity Python math.cos and math.acos function Python Project Ideas Based On Django Reverse a String in Python Reverse a Number in Python Python Word Tokenizer Python Trigonometric Functions Python try catch exception GUI Calculator in Python Implementing geometric shapes into the game in python Installing Packages in Python Python Try Except Python Sending Email Socket Programming in Python Python CGI Programming Python Data Structures Python abstract class Python Compiler Python K-Means Clustering NSE Tools In Python Operator Module In Python Palindrome In Python Permutations in Python Pillow Python introduction and setup Python Functionalities of Pillow Module Python Argmin Python whois Python JSON Schema Python lock Return Statement In Python Reverse a sentence In Python tell() function in Python Why learn Python? Write Dictionary to CSV in Python Write a String in Python Binary Search Visualization using Pygame in Python Latest Project Ideas using Python 2022 Closest Pair of Points in Python ComboBox in Python Python vs R Best resources to learn Numpy and Pandas in python Check Letter in a String Python Python Console Python Control Statements Convert Float to Int in Python using Pandas Difference between python list and tuple Importing Numpy in Pycharm Python Key Error Python NewLine Python tokens and character set Python Strong Number any() Keyword in python Best Database in Python Check whether dir is empty or not in python Comments in the Python Programming Language Convert int to Float in Python using Pandas Decision Tree Classification in Python End Parameter in python __GETITEM__ and __SETITEM__ in Python Python Namespace Python GUI Programming List Assignment Index out of Range in Python List Iteration in Python List Index out of Range Python for Loop List Subtract in Python Python Empty Tuple Python Escape Characters Sentence to python vector Slicing of a String in Python Executing Shell Commands in Python Genetic Algorithm in python Get index of element in array in python Looping through Data Frame in Python Syntax of Map function in Python After Python What Should I Learn Python AIOHTTP Alexa Python Artificial intelligence mini projects ideas in python Artificial intelligence mini projects with source code in Python Find whether the given stringnumber is palindrome or not First Unique Character in a String Python Python Network Programming Python Interface Python Multithreading Python Interpreter Data Distribution in python Flutter with tensor flow in python Front end in python Iterate a Dictionary in Python Iterate a Dictionary in Python – Part 2 Allocate a minimum number of pages in python Assertion Errors and Attribute Errors in Python Checking whether a String Contains a Set of Characters in python Python Control Flow Statements *Args and **Kwargs in Python Bar Plot in Python Conditional Expressions in Python Function annotations() in Python How to Write a Configuration file in Python Image to Text in python import() Function in Python Import py file in Python Multiple Linear Regression using Python Nested Tuple in Python Python String Negative Indexing Reading a File Line by Line in Python Python Comment Block Base Case in Recursive function python ER diagram of the Bank Management System in python Image to NumPy Arrays in Python NOT IN operator in Python One Liner If-Else Statements in Python Sklearn in Python Python Ternary Operators Self in Python Python vs Java Python Modulo Python Packages Python Syntax Python Uses Python Bitwise Operators Python Identifiers Python Matrix Multiplication Python AND Operator Python Logical Operators Python Multiprocessing Python Unit Testing __init__ in Python Advantages of Python Is Python Case-sensitive when Dealing with Identifiers Python Boolean Python Call Function Python History Python Image Processing Python main() function Python Permutations and Combinations Difference between Input() and raw_input() functions in Python Conditional Statements in python Confusion Matrix Visualization Python Python Algorithms Python Modules List Difference between Python 2 and Python 3 Is Python Case Sensitive Method Overloading in Python Python Arithmetic Operators Assignment Operators in Python Is Python Object Oriented Programming language Division in Python Python exit commands Continue And Pass Statements In Python Colors In Python Convert String Into Int In Python Convert String To Binary In Python Convert Uppercase To Lowercase In Python Convert XML To JSON In Python Converting Set To List In Python Covariance In Python CSV Module In Python Decision Tree In Python Difference Between Yield And Return In Python Dynamic Typing In Python

How to

How to Substring a String in Python How to Iterate through a Dictionary in Python How to convert integer to float in Python How to reverse a string in Python How to take input in Python How to install Python in Windows How to install Python in Ubuntu How to install PIP in Python How to call a function in Python How to download Python How to comment multiple lines in Python How to create a file in Python How to create a list in Python How to declare array in Python How to clear screen in Python How to convert string to list in Python How to take multiple inputs in Python How to write a program in Python How to compare two strings in Python How to create a dictionary in Python How to create an array in Python How to update Python How to compare two lists in Python How to concatenate two strings in Python How to print pattern in Python How to check data type in python How to slice a list in python How to implement classifiers in Python How To Print Colored Text in Python How to develop a game in python How to print in same line in python How to create a class in python How to find square root in python How to import numy in python How to import pandas in python How to uninstall python How to upgrade PIP in python How to append a string in python How to open a file in python How to Open a file in python with Path How to run a Python file in CMD How to change the names of Columns in Python How to Concat two Dataframes in Python How to Iterate a List in Python How to learn python Online How to Make an App with Python

Sorting

Python Sort List Sort Dictionary in Python Python sort() function Python Bubble Sort

Programs

Factorial Program in Python Prime Number Program in Python Fibonacci Series Program in Python Leap Year Program in Python Palindrome Program in Python Check Palindrome In Python Calculator Program in Python Armstrong Number Program in Python Python Program to add two numbers Anagram Program in Python Even Odd Program in Python GCD Program in Python Python Exit Program Python Program to check Leap Year Operator Overloading in Python Pointers in Python Python Not Equal Operator Raise Exception in Python Salary of Python Developers in India What is a Script in Python

Multiprocessing in python

The word multiprocessing is used to compute the operation in which more than two processors run simultaneously with more than one central processor. The computer's performance increases gradually. Multiprocessing is the ability of a system to run more than two processors simultaneously. The main application in multiprocessing will split into small threads and will run independently. These threads are allocated to the processors to increase the computer's performance.

Why do we Use multiprocessing?

Let’s consider a single processor in a computer system. When we assign multiple processes simultaneously, it takes more time, and it will disturb each task and move to others, which will be repeated until every process is completed.

This matches a housewife working at the workplace as well as at home. She has to look after both office work and household work.

The problem is that it is more critical to track all of them when you assign more processes to a single computer, and each process must run on time; this becomes more difficult. So within a single computer, multiprocessing uses more than two CPUs. In the operating system, every process is allocated to the processor.

  1. Multiprocessing has more than one central processor, which is represented by a computer.
  2. The multicore processor has a single computing component with two or more independent units.

How to Perform Multiprocessing in Python

To perform multiple tasks in a single system, python provides a multiprocessing module. It provides API and is user-friendly to work with multiprocessing.

EXAMPLE

# Import  multiprocessing module
import multiprocessing


def print_sum(num):
    """
    function to print the sum of given num
    """
    print ("sum: {}". format (num + num ))


def print_mul(num):
    """
    function to print mul of given num
    """


Print ("mul: {}". format (num * num))


if __name__ == "__main__":


    p1 = multiprocessing. Process (target=print_ sum, args = (15,))
    p2 = multiprocessing. Process (target=print_ mul, args= (15,))




    p1. start ()


    p2. start ()




    p1. join ()


    p2. join ()


    # Both processes finished
    print("Done!")
Square: 1

Output

Done!

Explanation

  1. To import the module of multiprocessing, we use import multiprocessing.
  2. For creating a process, we must create an object of a process class. It contains the following arguments:
    • Target: This is the function that the process will execute.
    • Args:  In this, the arguments will be passed to the target function.
    • From the above example, we have created two processes, namely p1 and p2.
    • p1 = multiprocessing. Process (target=print _sum, args=(10,))
    • p2 = multiprocessing. Process (target=print_ mul, args=(10,))
  3. We use the start method to start a process.
    • P1. start ()
    • P2. start ()
  4. The present program will execute only when the program starts. Using the join method stops the execution of the current program until and unless the process is completed.
    • P1. join ()
    • P2. join ()
  5. From the above-written program, until the completion of p1 and p2 current program will stop, and the following statements of the present program are executed once they are finished.

Communication between Processes

Communication is nothing but exchanging information between two sources. Without communication between each other or each resource, it isn't easy to carry out a task. Here in our topic, the data must be exchanged between processes to develop the parallel application. Processes use queues mostly to pass messages back and forth to other processes.

The below flowchart shows different communications mechanisms for the synchronization between multiple subprocesses. There are two types of multiprocessing, such as:

  1. Asymmetric multiprocessing
  2. Symmetric multiprocessing

Asymmetric Multiprocessing

In asymmetric multiprocessing, there will be the use of two or more processors that only one master processor handles. The CPUs are connected but are not self-scheduling. This is used to schedule a particular task to the CPU based on its importance and priority of tasks.

The processor's precedence is not equal. It is simpler to design and cheaper to implement. The processors are not treated equally in asymmetric multiprocessing. The main purpose of the master processor is to complete tasks of an operating system. 

Symmetric Multiprocessing

Symmetric multiprocessing uses two or more self-scheduling processors that share only a single and common memory space. Here every processor has access to memory devices and input and output. Symmetric multiprocessing uses two or more CPUs for a task and should complete it faster and more parallelly.

The processors are equally treated in symmetric multiprocessing. For communication between processors, shared memory will be used. Each processor is responsible for tasks of the operating system.

Different Communication Mechanisms

Queues, pipes, managers, and Ctypes are the different communication mechanisms. Firstly, we will learn about Queues.

Multiprocessing in Python
  1. QUEUES

    Multi-process programs use a queue communication mechanism for processing data between processes. API is used in multiprocessing because the queue class of the multiprocessing module is similar to the queue. As we know, the queue follows FIFO (First-in, first-out) mechanism, meaning the data element added first will be removed first, and the data element added last will be removed last.

    For communicating between processes, the queue provides a thread and also the process-safe FIFO mechanism for communication between processes.

For example,

# Importing Queue Class 


from multiprocessing import Queue  


cars = ['Benz', 'Audi', 'Kia', 'Tata', 'Skoda']  
count = 1  
# Creating a queue object  
queue = Queue ()  
print ('pushing items to the queue:')  
for c in cars:  
   print ('item no: ', count, ' ', 
   queue. put(c)  
   count += 1  


print ('\npopping items from the queue:')  
count = 0  
while not queue.empty(): 
   print ('item no: ', count, ' ', queue.get())  
   count += 1  


Output

Multiprocessing in Python

The above program shows we get to push and pop operations using a multiprocessing queue. We have imported the class of queue and a list of car names, and also, we had assigned count to 1; this count variable will count the total number of elements in the list.

Next, we created a queue object using the Queue () method. In for loop, we have inserted elements in order so that it performs operations in the queue.

  • PIPES

    Pipes are data structures that communicate between processes like multi-process programs. The function named pipe () will return a pair of connection objects which are connected by a pipe that has two ways called send () and receive ().

    For communicating between processes, a pipe acts as a medium of communication. A pipe can be within communication between parent and child processes or one process.

    In other words, communication can be hierarchical, like between grandparents, parents, children, etc. Communication between processes can be achieved by writing one process into the pipe and reading another process from the pipe. It works the same as a pipe system call that is one file to write and another to read from that file.

    Let us take a general scenario, such as plating trees by using a pipe here, plants will absorb water that is coming from the pipe, and we are pouring water with a pipe the same as above.
Multiprocessing in Python
  • MANAGER
    The manager is a class of multiprocessing modules that provides a way to process the information shared between all users. The object is a manager that controls shared objects and allows other processes to duplicate them. We can also say that manager is a communication module used to create data that share between processes of various types. 
    There are different properties of manager object; they are:
    • To control the server process, the manager manages the shared objects.
    • It has an important property that updates shared objects when a process modifies them.
  • C-TYPES Arrays and Values
    A c-type module provides tools for working with c datatypes. These c-type modules allow python to read and write with data using standard datatypes.

    The multiprocessing concept provides Array and Value objects for storing data in a shared memory map. Here Array is a c-type array allocated from shared memory, and Value is a c-type object allocated from shared memory.



ADVERTISEMENT
ADVERTISEMENT