Search code examples
pythonappendlist-comprehensionpython-multiprocessingmultiprocessing-manager

Even using Manager.list() the values are not being appended to the list using multiprocessing pool


As can see in the code below, I'm generating a multiprocessing with manager and list comprehension to handle the pool:

import multiprocessing

def foo(name,archive):
    print('hello ', name)
    archive.append('hello ', name)

def main():
    max_process = multiprocessing.cpu_count()-1 or 1
    pool = multiprocessing.Pool(max_process)
    manager = multiprocessing.Manager()

    archive = manager.list()

    [pool.apply_async(foo, args=[name,archive]) for name in range(0, 10)]
    pool.close()
    pool.join()
    print(archive)

if __name__ == '__main__':
    main()

But still the attachments are not being placed on the list designed by the manager:

hello  0
hello  4
hello  5
hello  7
hello  1
hello  6
hello  8
hello  3
hello  9
hello  2
[]

How should I go about solving this problem?


Solution

  • Try to change archive.append('hello ', name) to:

    archive.append(f"hello {name}")
    

    Then the result is:

    hello  0
    hello  5
    hello  1
    hello  9
    hello  6
    hello  3
    hello  7
    hello  8
    hello  2
    hello  4
    ['hello 0', 'hello 5', 'hello 1', 'hello 6', 'hello 9', 'hello 7', 'hello 3', 'hello 8', 'hello 2', 'hello 4']