Python multiprocessing - an easy way to implement a simple counter?

Hi everyone, I am currently using multiprocessing in python. and I’m just wondering if there is some simple counter variable, that each process, when they are executed, processes a certain task, can simply increase (like how much has been done as a whole).

I was looking for an API for Value, I don’t think it has changed.

+9
python multiprocessing
Aug 05 '09 at 13:19
source share
1 answer

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) # defaults to 0 counter_lock = Lock() def increment(): with counter_lock: counter.value += 1 def do_something(): print("I'm a separate process!") increment() Process(target=do_something).start() sleep(1) print counter.value # prints 1, because Value is shared and mutable 

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
+23
Aug 05 '09 at 13:46
source share



All Articles