enteracity
json_wuwu
2019-03-21 17:09
采纳率: 50%
浏览 353

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条回答 默认 最新

  • weixin_44705701
    有只猿叫Alan 2019-03-25 01:02
    已采纳

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

    点赞 评论

相关推荐