Processes in Python

3

I am studying the multiprocessing module and in all the examples in the Python documentation There is always a% checking in the examples, I know this checks to see if the file is being executed directly or being imported but has some relation to the initialization of a if ?

  

The multiprocessing module also introduces APIs which do not have   analogs in the threading module. The prime example of this is the Pool   object which offers a convenient means of parallelizing the execution   of a function across multiple input values, distributing the input   data across processes (data parallelism). The following example   demonstrates the common practice of defining such functions in a   module so that child processes can successfully import that module.   This basic example of data parallelism using Pool,

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))
    
asked by anonymous 08.08.2018 / 19:49

1 answer

1

This condition causes Pool to be started only if the program is called directly, as the multiprocessing module will also call this program to run the f() function for each instance of the pool this prevents the bootstrap from running a second time and producing an infinite loop .     

08.08.2018 / 21:03