mport threading
import time
def func(semaphore: threading.Semaphore, num):
# 获得信号量,信号量 -1
semaphore.acquire()
print(f"打印信号量:第{num}次")
time.sleep(3)
# 释放信号量,信号量 +1
semaphore.release()
if __name__ == '__main__':
# 初始化信号量,数量为 2
semaphore = threading.Semaphore(2)
# 运行4个线程
for num in range(4):
t = threading.Thread(target=func, args=(semaphore, num))
t.start()
标签:__,BoundedSemaphore,Semaphore,信号量,threading,num,semaphore,多线程
From: https://www.cnblogs.com/supergsy/p/17623737.html