pythonpython-3.xlistmultiprocessingmultiprocessing-manager

Problems using multiprocessing.manager


I was having some trouble using the manager. I want to have two processes accessing one list, one writing and one reading it. But the data was coming corrupted, so I tried to make an example to post here. Now, I have another problem. I'm passing a list as an argument to a function but the code doesn't work and says that I'm passing two arguments instead of one. Here is the code: import multiprocessing

def mde(dad):
    for i in range(100):
        for j in range(10):
            dad[0] = i
            dad[1] = j
def mda(dad):
    c = 0
    while c < 1001:
        print(dad)
        c += 1

if __name__ == '__main__':
    manager = multiprocessing.Manager()
    dado = manager.list([0, 0])
    print(dado)
    p1 = multiprocessing.Process(target=mde, args=dado)
    p2 = multiprocessing.Process(target=mda, args=dado)
    p1.start()
    p2.start()
    p1.join()
    p2.join()

Solution

  • Send your args as a tuple:

    import multiprocessing
    def mde(dad):
        for i in range(100):
            for j in range(10):
                dad[0] = i
                dad[1] = j
    def mda(dad):
        c = 0
        while c < 1001:
            print(dad)
            c += 1
    
    if __name__ == '__main__':
        manager = multiprocessing.Manager()
        dado = manager.list([0, 0])
        print(dado)
        p1 = multiprocessing.Process(target=mde, args=(dado,))
        p2 = multiprocessing.Process(target=mda, args=(dado,))
        p1.start()
        p2.start()
        p1.join()
        p2.join()