问题
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