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'

0

1个回答

tepe1 = threading.Lock这里应该出问题了,貌似LOCK是函数def allocate_lock()的引用,也就是说你需要用tepe1 = threading.Lock(),去创建tepe1这个互斥锁对象,说白了你就是写少了个括号...希望可以帮到你

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!