python 线程锁的使用

来源:互联网 发布:提高淘宝转化率 编辑:程序博客网 时间:2024/05/16 09:30

示例:

L,L1为list; x,y为object; D,D1为dict; i,j为int,且都为全局变量(公共资源)

不需要加锁:

L.append(x)L1.extend(L2)x = L[i]x = L.pop()L1[i:j] = L2L.sort()x = yx.field = yD[x] = yD1.update(D2)D.keys()


需要加锁:

i = i+1L.append(L[-1])L[i] = L[j]D[x] = D[x] + 1

源网页:http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm


1 0