Apr-14-2018, 03:55 PM
Ok, so I've rewritten your code a bit. You created a manager, but never passed it to the function. Being fast-and-loose with global variables can slide now and then, but not when dealing with multiple processes. Every single thing you use, needs to be passed in. This is a fairly advanced topic, so please don't take it poorly that you didn't get it right the first time (I've spent the past day reading docs just to get to this point, lol).
from functools import partial from multiprocessing import Pool, Manager def test(counter, lock, current_item): with lock: value = counter.get() + 1 counter.set(value) print(f"{value} => {current_item}") if __name__ == '__main__': list1 = ["item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9", "item10", "item11", "item12"] with Manager() as manager: value = manager.Value("i", 0) lock = manager.Lock() worker = partial(test, value, lock) with Pool(processes=4) as p: p.map(worker, list1)
Output:1 => item1
2 => item3
3 => item2
4 => item4
5 => item5
6 => item8
7 => item6
8 => item7
9 => item9
10 => item10
11 => item11
12 => item12