开发者

Python Multi-Processing -- Array Sharing?

开发者 https://www.devze.com 2023-03-18 01:56 出处:网络
When sharing a rawarray between diffe开发者_如何学JAVArent processes using multiprocessing, is it a problem to have all the children write / modify the raw array?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消