threading模块中有线程锁的功能,避免对公共资源操作导致的不可预计的后果
使用方法:
lock=threading.RLock 或者 lock=threading.Lock()
然后再线程中使用lock.acquire()方法加锁,用lock.release()解锁
比如:
def run(self):
global count
while not self.isstop:
lock.acquire()
count+=1
print 'thread %d-- count %d' %(self.no,count)
lock.release()
time.sleep(self.interval)
保证线程中的count变量安全操作