Share a list between different processes?

前端 未结 2 917
你的背包
你的背包 2020-12-05 11:23

I have the following problem. I have written a function that takes a list as input and creates a dictionary for each element in the list. I then want to append this dictiona

相关标签:
2条回答
  • 2020-12-05 12:10

    One way is to use a manager object and create your shared list object from it:

    from multiprocessing import Manager, Pool
    
    input_list = ['A', 'B', 'C', 'D', 'E', 'F']
    
    manager = Manager()
    shared_list = manager.list()
    
    def do_stuff(element):
        global shared_list
        element_dict = {}
        element_dict['name'] = element
        shared_list.append(element_dict)
        if len(shared_list) > 3:
            print('list > 3')
    
    pool = Pool(processes=6)
    pool.map(do_stuff, input_list)
    pool.close()
    

    Remember, unlike threads, processes do not share memory space. (When spawned, each process gets its own copy of the memory footprint of the spawning process, and then runs with it.) So they can only communicate via some form of IPC (interprocess communication). In Python, one such method is multiprocessing.Manager and the data structures it exposes, e.g. list or dict. These are used in code as easily as their built-in equivalents, but under the hood utilize some form of IPC (sockets probably).

    0 讨论(0)
  • 2020-12-05 12:11

    the following is from python documentation:

    from multiprocessing import shared_memory
    a = shared_memory.ShareableList(['howdy', b'HoWdY', -273.154, 100, None, True, 42])
    [ type(entry) for entry in a ]
    [<class 'str'>, <class 'bytes'>, <class 'float'>, <class 'int'>, <class 'NoneType'>, <class 'bool'>, <class 'int'>]
    a[2]
    -273.154
    a[2] = -78.5
    a[2]
    -78.5
    a[2] = 'dry ice'  # Changing data types is supported as well
    a[2]
    'dry ice'
    a[2] = 'larger than previously allocated storage space'
    Traceback (most recent call last):
      ...
    ValueError: exceeds available storage for existing str
    a[2]
    'dry ice'
    len(a)
    7
    a.index(42)
    6
    a.count(b'howdy')
    0
    a.count(b'HoWdY')
    1
    a.shm.close()
    a.shm.unlink()
    del a  # Use of a ShareableList after call to unlink() is unsupported
    
    0 讨论(0)
提交回复
热议问题