1. 程式人生 > >信號量 semaphore

信號量 semaphore

rt thread \n == read star 線程 %s pre count()


import threading, time


def run(n):
semaphore.acquire()
time.sleep(1)
print("run the thread: %s\n" % n)
semaphore.release()

if __name__ == ‘__main__‘:
semaphore = threading.BoundedSemaphore(5) # 最多允許5個線程同時運行
for i in range(22):
t = threading.Thread(target=run, args=(i,))
t.start()
while threading.active_count() != 1:
pass # print threading.active_count()
else:
print(‘----all threads done---‘)
#print(num)

信號量 semaphore