how to make a thread-safe global counter in python

前端 未结 2 417
面向向阳花
面向向阳花 2020-12-15 22:07

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 acces

相关标签:
2条回答
  • 2020-12-15 22:36

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

    Define a global lock:

    import threading
    threadLock = 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
    
    0 讨论(0)
  • The simplest solution is to protect the counter with a multiprocessing.Lock. I like to keep it in a class, like so:

    from multiprocessing import Process, RawValue, Lock
    import time
    
    class Counter(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()
    
        def increment(self):
            with self.lock:
                self.val.value += 1
    
        def value(self):
            with self.lock:
                return self.val.value
    
    def inc(counter):
        for i in range(1000):
            counter.increment()
    
    if __name__ == '__main__':
        thread_safe_counter = Counter(0)
        procs = [Process(target=inc, args=(thread_safe_counter,)) for i in range(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.

    0 讨论(0)
提交回复
热议问题