Search code examples
pythonmultithreadingworker

How to handle concurrent variable writes with Python workerpool


Is there a special way to use Python's workerpool to write to a variable that is outside a particular job.

Let's say, for example, you have a job called DownloadJob which downloads some file and increments a counter. What is the best way to handle the incrementing of this counter variable? Do I need to somehow lock the counter before writing to it or is this something that Python does automatically?

Thanks!


Solution

  • You can use threading.Lock for mutual exclusion. The documentation is pretty clear about them: http://docs.python.org/library/threading.html#lock-objects

    Cheers!