python 信号量男上加男,Event发射set与等待wait有点Y
程序员文章站
2022-04-02 11:06:34
相当于游泳池,只能容纳3个猛男,所以是18个人抢3个游泳席位,3个人玩刺激哈哈from threading import Thread,Semaphores = Semaphore(3)def run1(i): s.acquire() i=str(i) import time time.sleep(1) print("猛男{}正在大战".format(i)) s.release()if __name__...
相当于游泳池,只能容纳3个猛男,所以是18个人抢3个游泳席位,3个人玩刺激哈哈
from threading import Thread,Semaphore
s = Semaphore(3)
def run1(i):
s.acquire()
i=str(i)
import time
time.sleep(1)
print("猛男{}正在大战".format(i))
s.release()
if __name__ == '__main__':
for i in range(18):
t = Thread(target=run1,args=(i,))
t.start()
效果如下
补充event
from threading import Event,Thread
e = Event()
def man_cry():
print("ai")
e.wait()
def woman_scream():
e.set()
print('aaaaaaaaaaaaa')
if __name__ == '__main__':
for i in range(10):
t = Thread(target=man_cry)
t2 = Thread(target=woman_scream())
t.start()
t2.start()
本文地址:https://blog.csdn.net/wuyong15221125927/article/details/110940180