SharedCounter

giant.ufo.clearable_queue:

class giant.ufo.clearable_queue.SharedCounter(n=0)[source]

A synchronized shared counter.

The locking done by multiprocessing.Value ensures that only a single process or thread may read or write the in-memory ctypes object. However, in order to do n += 1, Python performs a read followed by a write, so a second process may read the old value before the new one is written by the first process. The solution is to use a multiprocessing.Lock to guarantee the atomicity of the modifications to Value.

This class comes almost entirely from Eli Bendersky’s blog: http://eli.thegreenplace.net/2012/01/04/shared-counter-with-pythons-multiprocessing/

Parameters:

n (int) –

property value: int

Return the value of the counter

Summary of Methods

increment

Increment the counter by n (default = 1)