信号量:
同一时间控制能并行执行的线程数:
释放一个,进去一个
threading.BoundedSemaphore()
同一时间控制能并行执行的线程数:
释放一个,进去一个
threading.BoundedSemaphore()
import threading, time
def run(n):
semaphore.acquire()
print("running task...", n)
time.sleep(5)
print("task %s done !" % n)
semaphore.release()
num = 0
semaphore = threading.BoundedSemaphore(5) #最多允许5个线程同时运行
for i in range(22):
t = threading.Thread(target=run, args=(i,))
t.start()
def run(n):
semaphore.acquire()
print("running task...", n)
time.sleep(5)
print("task %s done !" % n)
semaphore.release()
num = 0
semaphore = threading.BoundedSemaphore(5) #最多允许5个线程同时运行
for i in range(22):
t = threading.Thread(target=run, args=(i,))
t.start()
事件:Events
event = threading.Event()
a client thread can wait for the flag to be set
event.wait() #等待设定标志位
a server thread can set or reset it
event.set() 标志位被设定
event.clear() 标志位被清空,代表红灯,wait等待变绿灯
event.is_set() 判断是否设定了标志位
event.wait() 等待设定标志位
案例: 红绿灯汽车:
import time, threading
event = threading.Event()
def traffic_light():
count = 0
event.set()
while True:
if count >5 and count <=10: #改成红灯
event.clear() #把标志位清除
print(" 33[41;1mred light is on .... 33[0m")
elif count >10:
event.set() #变绿灯
count = 0
else:
print(" 33[42;1mred light is on .... 33[0m")
time.sleep(1)
count += 1
def car(name):
while True:
if event.is_set():
print("[%s] is running......" % name)
time.sleep(1)
else:
print("[%s] is waiting......, Red light" % name)
event.wait()
print(" 33[34;1mThe green light is on ,start going.... 33[0m")
light = threading.Thread(target=traffic_light)
light.start()
c1= threading.Thread(target=car, args=("telse", ))
c1.start()
案例2:
员工进公司要刷卡,设置门位一个线程,再设置几个线程为员工,
员工看到门,就刷卡,门开了,员工通过
import threading,time
event = threading.Event()
def gate():
while not event.is_set():
print("xxgate is close......")
event.wait()
def staff(name):
if event.is_set():
print("gate is open......")
print("%s pass to gate...." % name)
else:
print("%s brash id card to open gate...." % name)
event.set()
print("%s pass to gate...." % name)
event.clear()
time.sleep(0.1)
d = threading.Thread(target=gate)
d.start()
for n in ("brace", "kitty", "lucy"):
s = threading.Thread(target=staff, args=(n,))
s.start()
s.join()
员工进公司要刷卡,设置门位一个线程,再设置几个线程为员工,
员工看到门,就刷卡,门开了,员工通过
import threading,time
event = threading.Event()
def gate():
while not event.is_set():
print("xxgate is close......")
event.wait()
def staff(name):
if event.is_set():
print("gate is open......")
print("%s pass to gate...." % name)
else:
print("%s brash id card to open gate...." % name)
event.set()
print("%s pass to gate...." % name)
event.clear()
time.sleep(0.1)
d = threading.Thread(target=gate)
d.start()
for n in ("brace", "kitty", "lucy"):
s = threading.Thread(target=staff, args=(n,))
s.start()
s.join()
队列queue:
类似一个有顺序的容器,与类别区别是,数据只有一份,取走就没了
队列数据是放内存的
class queue.Queue(maxsize=0) #先进先出;
class queue.LifoQueue(maxsize=0) #后进先出;
class queue.PrioriyQueue(maxsize=0) #存储数据时可设置优先级的队列
1. 生成queue对象;
2. Queue.put(block=True, timeout=1) 放数据
3. Queue.get(block=True, timeout=1) 取数据
4. Queue.qsize() 读队列大小
5. Queue.full()
案例:
import queue
q = queue.Queue() #先进先出
q.put("a1")
q.put("a2")
q.put("a3")
q.put("a4")
print(q.qsize())
print(q.get())
print(q.get())
print(q.get())
print(q.get())
import queue
q = queue.Queue() #先进先出
q.put("a1")
q.put("a2")
q.put("a3")
q.put("a4")
print(q.qsize())
print(q.get())
print(q.get())
print(q.get())
print(q.get())
生成者消费者:
案例:
import queue, threading,time
q = queue.Queue()
def Producer(name):
for i in range(10):
print("[%s] 生产的骨头 %s" %(name, i))
q.put("骨头 %s" %i)
time.sleep(0.5)
def Consumer(name):
while q.qsize()>0:
print("[%s] 取到 [%s] 并吃了它...." % (name, q.get()))
time.sleep(1)
p = threading.Thread(target=Producer, args=("brace", ))
c = threading.Thread(target=Consumer, args=("kitty",))
案例:
import queue, threading,time
q = queue.Queue(10)
def Producer(name):
i = 0
while True:
print("[%s] 生产的骨头 %s" %(name, i))
q.put("骨头 %s" %i)
time.sleep(0.5)
i += 1
def Consumer(name):
while True:
print("[%s] 取到 [%s] 并吃了它...." % (name, q.get()))
#time.sleep(1)
p = threading.Thread(target=Producer, args=("brace", ))
c1 = threading.Thread(target=Consumer, args=("kitty",))
c2 = threading.Thread(target=Consumer, args=("lucy",))
p.start()
c1.start()
c2.start()