python parallel map (multiprocessing.Pool.map) with global data

后端 未结 2 1519
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 00:27

I\'m trying to call a function on multiple processes. The obvious solution is python\'s multiprocessing module. The problem is that the function has side effects.

2条回答
  •  盖世英雄少女心
    2021-02-02 01:02

    You need the list glob_data to be backed by shared memory, Multiprocessing's Manager gives you just that:

    import multiprocessing as multi
    from multiprocessing import Manager
    
    manager = Manager()
    
    glob_data = manager.list([])
    
    def func(a):
        glob_data.append(a)
    
    map(func,range(10))
    print glob_data  # [0,1,2,3,4 ... , 9] Good.
    
    p = multi.Pool(processes=8)
    p.map(func,range(80))
    
    print glob_data # Super Good.
    

    For some background:

    https://docs.python.org/3/library/multiprocessing.html#managers

提交回复
热议问题