python多線程,限制線程數


#encoding:utf8

import threading
import time

data = 0

def func(sleeptime):
    global data
    print threading.currentThread().getName()
    time.sleep(sleeptime)
threads = []

for i in range(0,40):
    t = threading.Thread(target=func,args=(i,))
    threads.append(t)

num = 0
for t in threads:
    t.start()
    while True:
        #判斷正在運行的線程數量,如果小於5則退出while循環,
        #進入for循環啟動新的進程.否則就一直在while循環進入死循環
        if(len(threading.enumerate()) < 5):
            break


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM