简体   繁体   中英

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM