Introduction

As in previous blog of “Introduction to multiprocessing”, we have discussed the following points.

1. Introduction to multiprocessing.
2. Why do we need multiprocessing?
3. Multiprocessing module in Python.

Now, we will discuss How to returned values from processes, Communication between processes.

For return values, we have a pool object in the multiprocessing module of Python which also handles multiple input values. It distributes the input data across processes also known as data parallelism.

Let’s see through the example of this pool object.

Pool Object

from multiprocessing import Pool
import multiprocessing 
Here we are defining one function to be used in the pool object in order to execute it for multiple values.
In [2]:
def frequency(x):
    return str("Hey this is your" + str(x)+ "time")
Here we are using this frequency function defined by us and it is executing three times as we give 3 values in parenthesis.
In [ 3]:
if __name__ == '__main__':
    p = Pool(5)
    print(p.map(frequency, [1, 2, 3]))

The output after executing this code is -:

Next, we will discuss How to exchange or communicate between processes.

Communication between two processes

Multiprocessing supports two type of communication-:

1. Queue
2. Pipe

1. Queue

def people(li, q): 

    for name in li: 
        q.put("Welcome "+ str(name)) 
  
def welcome_people(q): 

    print("Welcoming People: \n") 
    while not q.empty(): 
        print(q.get()) 
In [3]:
if __name__ == "__main__": 
    # input list 
    li = ['Ram','Geeta'] 
  
    # creating multiprocessing Queue 
    q = multiprocessing.Queue() 
  
    # creating new processes 
    p1 = multiprocessing.Process(target=people, args=(li, q)) 
    p2 = multiprocessing.Process(target=welcome_people,  args=(q,)) 
  
    # running process p1 to square list 
    p1.start() 
    p1.join() 
  
    # running process p2 to get queue elements 
    p2.start() 
    p2.join()

2. Pipe

def coming_people(conn, peoples): 

    for people in peoples: 
        conn.send(people) 
    conn.close() 
  
def welcoming_people(conn): 

    while True: 
        people = conn.recv()
        print("Welcome: {}".format(people)) 
In [ ]:
if __name__ == "__main__": 
    # People to be welcome
    peoples = ["Ram", "Geeta", "Rajesh"] 
  
    # creating a pipe 
    parent_conn, child_conn = multiprocessing.Pipe() 
  
    # creating new processes 
    p1 = multiprocessing.Process(target=coming_people, args=(parent_conn,peoples)) 
    p2 = multiprocessing.Process(target=welcoming_people, args=(child_conn,)) 
  
    # running processes 
    p1.start() 
    p2.start() 
  
    # wait until processes finish 
    p1.join() 
    p2.join()