'Python multiprocessing with macOs

I have a mac (MacOs 10.15.4, Python ver 3.82) and need to work in multiprocessing, but on my pc the procedures doesn’t work.

For example, I have copied a simple parallel python program

import multiprocessing as mp  
import time  

def test_function(i):   
    print("function starts" + str(i))  
    time.sleep(1)  
    print("function ends" + str(i))  

if __name__ == '__main__':  
    pool = mp.Pool(mp.cpu_count())  
    pool.map(test_function, [i for i in range(4)])  
    pool.close()  
    pool.join()  

What I expect to see in the output:

function starts0  
function ends0  
function starts1  
function ends1  
function starts2  
function ends2  
function starts3  
function ends3

Or similar...

What I actually see:

= RESTART: /Users/Simulazioni/prova.py    
>>> 

Just nothing, no errors and no informations, just nothing. I have already try mamy procedure without results. The main problem, I could see, is the call of the function, in fact the instruction:

if __name__ == '__main__':  

doesn’t call the function,

def test_function(i): 

I tried many example of that kind without results.

Is ti possible and/or what's is the easiest way to parallelize in macOs?



Solution 1:[1]

I know this is a bit of an old question, but I just faced the same issue and I solved it by using a different multithreading package's version, which is:

import multiprocess

instead of:

import multiprocessing

Reference:

https://pypi.org/project/multiprocess/

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Abud