Introduction to multiprocessing

Theory

Using pipe in multiprocessing

Report a typo

Write a Python program that uses the Pipe method from the multiprocessing library to perform the following operation:

  • Take a list of integers as input and find the sum and product of the integers using two separate child processes;
  • Then, send these results back to the parent process through the Pipe. The parent process should print the sum and product of the list.

Sample Input 1:

2 3 4

Sample Output 1:

Sum: 9
Product: 24
Write a program in Python 3
from multiprocessing import Process, Pipe

def calculate_sum(numbers, conn):
pass # Replace this line with your code


def calculate_product(numbers, conn):
pass # Replace this line with your code

if __name__ == "__main__":
parent_conn, child_conn = Pipe()

numbers = list(map(int, input().split()))

pass # Replace this line with your code

result1 = parent_conn.recv()
result2 = parent_conn.recv()

print(f"Sum: {result1}")
print(f"Product: {result2}")

Create a free account to access the full topic