python3.7 互斥锁问题
问题描述:
acquire(),release()Pycharm提示没有参数,原来不是不用传参数的吗?
import threading
import time
num =0
tepe1 = threading.Lock
def f1():
for i in range(1000000):
global num
tepe1.acquire()
num += 1
tepe1.release()
print(num)
def f2():
for y in range(1000000):
global num
tepe1.acquire()
num += 1
tepe1.release()
print(num)
#创建锁
t = threading.Thread(target=f1)
t1 = threading.Thread(target=f2)
t.start()
t1.start()
print(num)
结果:AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'
答
tepe1 = threading.Lock这里应该出问题了,貌似LOCK是函数def allocate_lock()的引用,也就是说你需要用tepe1 = threading.Lock(),去创建tepe1这个互斥锁对象,说白了你就是写少了个括号...希望可以帮到你