Python Multi-Processing -- Array Sharing?
When sharing a rawarray
between diffe开发者_如何学JAVArent processes using multiprocessing
, is it a problem to have all the children write / modify the raw array
?
Does one need to handle lockings etc in such a case?
From python documentation:
multiprocessing.sharedctypes.RawArray(typecode_or_type, size_or_initializer)
Return a ctypes array allocated from shared memory. typecode_or_type determines the type of the elements of the returned
array: it is either a ctypes type or a one character typecode of the kind used by the array module. If size_or_initializer is an integer then it determines the length of the array, and the array will be initially zeroed. Otherwise size_or_initializer is a sequence which is used to initialize the array and whose length determines the length of the array.
Note that setting and getting an element is potentially non-atomic;
use Array() instead to make sure that access is automatically synchronized using a lock.
So, you may need to use multiprocessing.sharedctypes.Array which allows locking and synchronization between processes.
精彩评论