我有几个工作进程应该在完成后减少一个共享整数。
为了确保线程安全,我尝试了一个锁,但不知何故它不起作用,在程序结束时打印出工作人员仍将输出 4
workers = 4
lock = threading.Lock()
def workerStopped(id,lock):
lock.acquire()
global workers
print "Decrease workers (" + str(workers) + ")for #" + str(id)
workers = workers - 1
print "Now " + str(workers) + " Workers"
lock.release()
class Worker(Process):
def __init__(self, queue,ident,lock):
super(Worker, self).__init__()
self.queue= queue
self.idstr= str(ident)
self.lock = lock
print "Ident" + self.idstr
......
workerStopped(self.idstr,self.lock)
....
for i in range(4):
Worker( request_queue,i,lock ).start()