Value really changed; you specify the data type you want from the ctypes module, and then you can mutate it. Here's the full script workflow that demonstrates this:
from time import sleep from ctypes import c_int from multiprocessing import Value, Lock, Process counter = Value(c_int)
EDIT: Luper correctly indicates in the comment below that Value values ββare locked by default. This is correct in the sense that even if an assignment consists of several operations (such as assigning a string that can be many characters), this assignment is atomic. However, when adding a counter, you still need an external lock, as shown in my example, because the increment loads the current value and then increments it and then returns the result back to Value .
Thus, without external blocking, you may encounter the following circumstance:
- Process 1 reads (atomically) the current value of the counter, then increments it
- before process 1 can assign an incremented counter back to
Value , a context switch occurs - Process 2 reads the (atomic) current (unlit) counter value, increments it and assigns the incremental result (from the atomic point of view) back to
Value - Process 1 assigns its incremental value (atomic), blowing off the increment performed by process 2
Eli Courtwright Aug 05 '09 at 13:46 2009-08-05 13:46
source share