Skip to content Skip to sidebar Skip to footer

How To Make A Thread-safe Global Counter In Python

I'm creating a threading.Timer(2,work) run threads. Inside each work function, upon some condition the global counter must increment without conflict for access of counter variable

Solution 1:

Not sure if you have tried this specific syntax already, but for me this has always worked well:

Define a global lock:

importthreadingthreadLock= threading.Lock()

and then you have to acquire and release the lock every time you increase your counter in your individual threads:

with threadLock:
    global_counter += 1

Solution 2:

One solution is to protect the counter with a multiprocessing.Lock. You could keep it in a class, like so:

from multiprocessing import Process, RawValue, Lock
import time

classCounter(object):
    def__init__(self, value=0):
        # RawValue because we don't need it to create a Lock:
        self.val = RawValue('i', value)
        self.lock = Lock()

    defincrement(self):
        with self.lock:
            self.val.value += 1defvalue(self):
        with self.lock:
            return self.val.value

definc(counter):
    for i inrange(1000):
        counter.increment()

if __name__ == '__main__':
    thread_safe_counter = Counter(0)
    procs = [Process(target=inc, args=(thread_safe_counter,)) for i inrange(100)]

    for p in procs: p.start()
    for p in procs: p.join()

    print (thread_safe_counter.value())

The above snippet was first taken from Eli Bendersky's blog, here.

Post a Comment for "How To Make A Thread-safe Global Counter In Python"