Values from Processes

Introduction

As in the 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

Here we will define one function(frequency) to be used in the pool object in order to execute it for multiple values.

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 types of communications:

1. Queue
2. Pipe

Something wrong, after execution of code no output – Recheck.

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()
Close Menu