简体   繁体   English

Python 与 macOs 的多处理

[英]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.我有一台 mac(MacOs 10.15.4、Python ver 3.82)并且需要在多处理中工作,但在我的电脑上这些程序不起作用。

For example, I have copied a simple parallel python program比如我复制了一个简单的并行python程序

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:我希望在 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.我已经尝试过 mamy 程序但没有结果。 The main problem, I could see, is the call of the function, in fact the instruction:我可以看到,主要问题是 function 的调用,实际上是指令:

if __name__ == '__main__':  

doesn't call the function,不呼叫 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?是否可能和/或在 macO 中并行化的最简单方法是什么?

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/ https://pypi.org/project/multiprocess/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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