您的位置:首页 > 编程语言 > Python开发

python多线程,限制线程数

2013-11-09 19:08 513 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: