json_wuwu 2019-03-21 17:09 采纳率: 0%
浏览 424
已采纳

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'

  • 写回答

1条回答 默认 最新

  • 有只猿叫Alan 2019-03-25 01:02
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?