semaphore信号量
from threading import Thread,Semaphore #类似锁的一个东西
import time
lock_num=Semaphore(2) #设置锁的数量
def test(name):
lock_num.acquire()
print(f'{name}已加入')
time.sleep(2)
print(f'{name}已离开')
lock_num.release()
if __name__ == '__main__':
l=['马云','马化腾','马斯克','马保国',]
for name in l:
t=Thread(target=test,args=(name,))
t.start()

浙公网安备 33010602011771号