Python多重处理的Pool常数

时间:2019-11-16 10:04:22

标签: python multiprocessing constants pool

我是python中的多处理模块的初学者,我只想对 def func 使用并发执行。此外,我在代码中使用了一些常量,但我对它们有疑问。 代码是(python 3.6.8):

from multiprocessing import Pool

FIRST_COUNT=10
print("Enter your path")
PATH=input()

some_list=[]
for i in range(10000):
    some_list.append(i)  


def func(some_list):
    .....


if __name__ == "__main__":
chunks = [some_list[i::4] for i in range(4)]
pool = Pool(processes=4)

pool.map(func,chunks)

当我尝试启动此程序时,我看到消息输入您的路径5次,而我需要输入5次。即此代码执行1 + 4(每个进程)次。

我想像常量一样使用FIRST_COUNT,PATH和some_list,并且仅对 func 使用多处理。我怎样才能做到这一点。请帮帮我。

1 个答案:

答案 0 :(得分:0)

您应该将代码放入if __name__ == "__main__":中,以仅执行一次

if __name__ == "__main__":
    FIRST_COUNT = 10
    PATH = input("Enter your path: ")

    some_list = list(range(10000))
    #some_list = []
    #for i in range(10000):
    #    some_list.append(i)  

    chunks = [some_list[i::4] for i in range(4)]
    pool = Pool(processes=4)

    results = pool.map(func, chunks)

    print(results)

如果您想使用FIRST_COUNTPATH,则最好将其作为参数发送到func

您将必须使用FIRST_COUNT中的PATHchunks创建元组

chunks = [(FIRST_COUNT, PATH, some_list[i::4]) for i in range(4)]

函数必须将其作为元组获取并解压缩

def func(args):
    first_count, paht, some_list = args

工作示例

from multiprocessing import Pool

def func(args):
    first_count, path, some_list = args

    result = sum(some_list)

    print(first_count, path, result)

    return result 


if __name__ == "__main__":
    FIRST_COUNT = 10
    PATH = input("Enter your path: ")

    some_list = list(range(10000))
    #some_list = []
    #for i in range(10000):
    #    some_list.append(i)  

    chunks = [(FIRST_COUNT, PATH, some_list[i::4]) for i in range(4)]
    pool = Pool(processes=4)

    all_results = pool.map(func, chunks)

    print('all results:', all_results)

编辑:您也可以使用starmap()代替map()

    all_results = pool.starmap(func, chunks)

然后您可以使用(不带解包参数)

def func(first_count, path, some_list):