码迷,mamicode.com
首页 > 其他好文 > 详细

信号量 semaphore

时间:2018-11-18 16:11:56      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:线程   def   port   start   pre   count   信号   lease   tar   


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

标签:线程   def   port   start   pre   count   信号   lease   tar   

原文地址:https://www.cnblogs.com/rongye/p/9977983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!