python 中的semaphore

来源:互联网 发布:人工智能简史 pdf 编辑:程序博客网 时间:2024/06/05 11:18

控制并发的线程数

#-*-coding:utf-8-*- #__author:martin#date:2017/10/22import  threadingimport  timeclass Mythread(threading.Thread):    def run(self):       if semaphore.acquire():           print(self.name)           time.sleep(5)           semaphore.release()if __name__ == '__main__':      semaphore = threading.BoundedSemaphore(5)      threads = []      for i in range(100):          threads.append(Mythread())      for t in threads:          t.start()
原创粉丝点击