欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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()

效果如下
python 信号量男上加男,Event发射set与等待wait有点Y
补充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()

python 信号量男上加男,Event发射set与等待wait有点Y

本文地址:https://blog.csdn.net/wuyong15221125927/article/details/110940180

相关标签: python基础