Let's start with an example:
Subclass Thread :
import threading class Dev(threading.Thread): def __init__(self, workQueue, queueLock, count): super(Dev, self).__init__()
The with statement is a smart way to get and release a lock, see doc .
Now the current code:
import Queue import time work_q = Queue.Queue()
With the above code, we have a clear example of how self.count remains unchanged no matter what we do with count .
The reason for this behavior is that the call:
dev = Dev(work_q, q_lock, count)
or
dev = Dev(work_q, q_lock, 1)
- same.
Arnold Moon self.count way to change self.count . Applying this to our example:
class Dev(threading.Thread): def __init__(self, workQueue, queueLock, count): super(Dev, self).__init__() self.workQueue = workQueue self.queueLock= queueLock self.count = count def set_count(self, value): self.count = value def run(self): data = '' while 1: with self.queueLock: if not self.workQueue.empty(): data = self.workQueue.get() print data print self.count if data == 'quit': break
Calling set_count in our current code will change the value of self.count :
time.sleep(1) with q_lock: work_q.put('word')
Hope this helps you clarify some doubts.