Srijan Joshi
Srijan Joshi

Reputation: 48

Python multiprocessing list of locks

I'm using the multiprocessing.Array module to share a very large Array across all my child processes. The array can be variable length.

I need to synchronize access in every element in the array. So, I do not want to block write operations when two different indexes are written separately. I think this will occur if I use the default (lock = True) in multiprocessing.Array.

Is there any way to have a lock/mutex for each variable in the array?

Thanks in advance

Upvotes: 0

Views: 437

Answers (1)

Srijan Joshi
Srijan Joshi

Reputation: 48

As @MisterMiyagi said I could just pass a list of Values to the function. Below is a dummy code

import multiprocessing
import ctypes


def calc_square(numbers):
    for idx, n in enumerate(numbers):
        numbers[idx] = (idx)

if __name__ == "__main__":
    numbers = [multiprocessing.Value('d', 0.0) for i in range(100)]
    p = multiprocessing.Process(target = calc_square, args = (numbers,))

    p.start()
    p.join()

The child process will update each value and it can be synchronized.

Upvotes: 1

Related Questions