giorgosp
giorgosp

Reputation: 426

Python multiprocessing lock strange behavior

I notice a behaviour in my code that I cannot explain. This is the code:

import multiprocessing
from collections import deque

LOCK = multiprocessing.Lock()

data = deque(['apple', 'orange', 'melon'])

def f(*args):
    with LOCK:
        data.rotate()
        print data[0]

pool = multiprocessing.Pool()
pool.map(f, range(4))

I expect that the output would be

melon
orange
apple
melon

but instead I get

melon
melon
melon

Any ideas would be greatly appreciated.

Upvotes: 0

Views: 75

Answers (1)

Albert
Albert

Reputation: 68350

As Tim Peters commented, the problem is not the Lock but that the deque is not shared across the processes but every process will have their own copy.

There are some data structures provided by the multiprocessing module which will be shared across processes, e.g. multiprocessing.Queue. Use that instead.

Upvotes: 1

Related Questions