Python multiprocessing lock strange behavior
Question: 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.


Answer: 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`](https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Queue).
Use that instead.

