'Python multiprocessing - pipe find prime numbers in an interval

In python 3, I have to find prime numbers in an interval [a,b] using multiprocessing pipes in specific... Here is my code using the pool.map method:

import multiprocessing
import time

def isprime(num):
    if num < 2:
        return None
    for i in range(2, num):
         if (num % i) == 0:
            return None
    else:
        return num

if __name__ == "__main__":
    pool = multiprocessing.Pool(3)
    start_time = time.perf_counter()
    result = list(filter(lambda x: x is not None, pool.map(isprime, range(0,30))))
    finish_time = time.perf_counter()
    print(f"Program finished in {finish_time-start_time} seconds")
    print(result)

i didnt fully understand the pipe method here is the code tht i found as an example while doing my research

from multiprocessing import Process, Pipe

def parentData(parent):
    ''' This function sends the data for the child process '''
    parent.send(['Hello'])
    parent.close()

def childData(child):
    ''' This function sends the data for the parent process '''
    child.send(['Bye'])
    child.close()

if __name__ == '__main__':
    parent, child = Pipe()              # Create Pipe
    process1 = Process(target = parentData, args = (parent, ))      # Create a process for handling parent data
    process2 = Process(target = childData, args = (child, ))        # Create a process for handling child data
    process1.start()                    # Start the  parent process
    process2.start()                    # Start the child process
    print(parent.recv())                # Display data received from child (BYE)
    print(child.recv())                 # Display data received from parent (HELLO)
    process1.join()                     # Wait till the process completes its execution
    process2.join()

Can anyone show me an example of doing the multiprocessing of prime numbers with pipes?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source