信号量


 1 import threading,time
 2 
 3 def  run(n):
 4     semaphore.acquire()
 5     time.sleep(1)
 6     print("run in the %s thread"%n)
 7     semaphore.release()
 8 
 9 if __name__ ==  '__main__':
10     semaphore = threading.BoundedSemaphore(5)#最多允许5个线程同时执行
11     for i in range(22):
12         t = threading.Thread(target=run,args=(i,))
13         t.start()
14 
15 while threading.active_count() != 1:
16     pass# print("active count :",threading.active_count())
17 
18 else:
19     print("all threads done")
原文地址:https://www.cnblogs.com/cerofang/p/8013106.html