How to share a synchronised list of classes between processes in python multiprocessing

坚强是说给别人听的谎言 提交于 2019-12-02 16:02:56

问题


How do i share state and synchronize a list of classes when using multiprocessing.

I have a crude example below where i am increasing the amount of 3 peoples wallets. The 3 people all have a different starting amounts. I iterate 10 times and give them an extra 10 each time.

import concurrent.futures
import multiprocessing as mp
from multiprocessing import Manager
from ctypes import py_object

class Wallet():

    def __init__(self, name, amount):
        self.name = name
        self.amount = amount

def give(num):

    for w in _WALLETLIST:

        # with l.get_lock():
        w.amount += num
        print("Name: ", w.name, " Amount: ", w.amount)


def init_globals(walletList):
    global _WALLETLIST
    _WALLETLIST = walletList

def main():

    wallet1 = Wallet("John", 100)
    wallet2 = Wallet("Alice", 200)
    wallet3 = Wallet("Bob", 300)
    amount_per_week = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

    walletList = [wallet1, wallet2, wallet3]

    mp_walletList = mp.Array(py_object, walletList)

    with concurrent.futures.ProcessPoolExecutor(initializer=init_globals, initargs=(mp_walletList,)) as executor:
        for _ in executor.map(give, amount_per_week):
            pass
    print()


if __name__ == "__main__":
    main()

At the end i expect John to have 200, Alice to have 300 and Bob to have 400.

I have tried multiprocessing.Array but i've not found a way to pass in a class so that it is shared and synchronised.

来源:https://stackoverflow.com/questions/56092001/how-to-share-a-synchronised-list-of-classes-between-processes-in-python-multipro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!