from multiprocessing import Process, Pipe def send_data(conn): conn.send('Hello from child process') if __name__ == '__main__': parent_conn, child_conn = Pipe() process = Process(target=send_data, args=(child_conn,)) process.start() print(parent_conn.recv()) process.join()
from multiprocessing import Process, Pipe def square_nums(numbers, conn): for number in numbers: conn.send(number * number) conn.close() if __name__ == '__main__': parent_conn, child_conn = Pipe() process = Process(target=square_nums, args=([1, 2, 3, 4, 5], child_conn)) process.start() while True: try: print(parent_conn.recv()) except EOFError: break process.join()In this example, we pass a list of numbers to a child process and send their square values back to the parent process. We use a `for` loop in the child process to iterate over the list of numbers and send their square values using the `send()` method on the `conn` object. In the parent process, we use a `while` loop to continuously receive messages from the child process until the `recv()` method raises an EOFError exception indicating that no more data is being sent through the pipe. The multiprocessing module is a package library that comes with Python's standard library.