编程语言
首页 > 编程语言> > python-lock

python-lock

作者:互联网

import threading
import time
import logging
logging.basicConfig(level=logging.INFO)

cups = []
lock = threading.Lock() #锁

def factory():
logging.info("杯子的数量:{}".format(cups))

def worker(n,lock):
while True:
lock.acquire() #请求到锁
count = len(cups)
logging.info("杯子数量:{}".format(count))
time.sleep(0.6)
if count >= n:
break
cups.append(1)
lock.release() # 释放锁
logging.info("{} make one".format(threading.current_thread().name))
logging.info("当前杯子数:{}".format(len(cups)))

for i in range(10):
t = threading.Thread(target=worker,args=(100,lock))
t.start()

标签:info,count,logging,format,python,lock,cups
来源: https://www.cnblogs.com/centos-kang/p/12251934.html