使用池和映射进行多处理问题



我想通过在python中使用池和映射函数来获得输出,如下所示:

**按随机顺序输出:

PoolWorker-10输出:[0,4,8,12,16,20]

PoolWorker-11输出:[0,4,8,12,16,20]

PoolWorker-12输出:[0,4,8,12,16,20]

PoolWorker-1输出:[0,4,8,12,16,20]

…..**

我试过几次把输出结果和current_process名称写在一行中,但都失败了。我不知道应该更改哪个才能得到上面打印的输出。

from multiprocessing import Pool, current_process
def multiprocessing_func():
input_list = [0, 1, 2, 3, 4, 5]
print("input list: {}".format(input_list))
print("Output in random order: ")
pool = Pool(10, current_process_func)
result = pool.map(calculate, input_list)
print(result)
pool.close()
pool.join()
def calculate(y_list):
return y_list * 4
def current_process_func():
print("{} output: ".format(current_process().name))
if __name__ == '__main__':
multiprocessing_func()

当我运行它时,我得到如下结果:

按随机顺序输出:

PoolWorker-1输出:

PoolWorker-2输出:

PoolWorker-3输出:

PoolWorker-4输出:

PoolWorker-5输出:

PoolWorker-7输出:

PoolWorker-6输出:

[0,4,8,12,16,20]

PoolWorker-8输出:

PoolWorker-9输出:

PoolWorker-10输出:


我应该更改什么才能得到上面我想要的结果?

更改了您的代码片段,如下所示。总体考虑如下:

  • pool.map(func, data)数据应理解为要分布在整个cpu池中的数据(也是该进程的专有数据(。此外,至少应该有与流程一样多的项目,以最大限度地利用池。(我只列出了10个相同的列表(。

  • 跳过CCD_ 2构造中的初始化参数,以便在计算完成后仅输出所需的数据。

    from multiprocessing import Pool, current_process
    def multiprocessing_func():
    input_list = [0, 1, 2, 3, 4, 5]
    print("input list: {}".format(input_list))
    print("Output in random order: ")
    pool = Pool(10) 
    result = pool.map(calculate, [input_list]*10)
    print()
    print("collected:" , result)
    pool.close()
    pool.join()
    def calculate(y_list):
    out = [x*4 for x in y_list] 
    current_process_func(out)
    return out   
    def current_process_func(data):
    print("{} output: {}".format(current_process().name, data))
    if __name__ == '__main__':
    multiprocessing_func()
    

导致:

input list: [0, 1, 2, 3, 4, 5]
Output in random order: 
ForkPoolWorker-52 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-55 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-57 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-51 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-53 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-54 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-56 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-58 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-60 output: [0, 4, 8, 12, 16, 20]
ForkPoolWorker-59 output: [0, 4, 8, 12, 16, 20]
collected: [[0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4,    8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20], [0, 4, 8, 12, 16, 20]]

这是你所期望的吗?

相关内容

  • 没有找到相关文章

最新更新