python多线程测试(线程状态跟踪)

#!/usr/bin/python
# coding:utf-8
import MySQLdb
import sys,re,time,random,threading,os
from db import *
reload(sys)
sys.setdefaultencoding('utf-8')

#多线程测试
def test():
    fto = random.randrange(5,30)
    for i in range(1,fto):
        print i
        time.sleep(0.5)

def check_thread(threads):
    is_working = 0;
    for t in threads:
        if t.isAlive():
            #print(t.name+str('正在运行'))
            is_working+=1
        else:
            print(t.name+str('运行完成'))
            pass
    return is_working

threads = []
for i in range(1,10):
    t = threading.Thread(target=test)
    t.name = 'w'+str(i)
    t.start()
    threads.append(t)

while True:
    check_result = check_thread(threads)
    if check_result == 0:
        print('所有线程运行线束')
        break;
    else:
        #print(str(check_result)+'个线程在运行')
        pass
    time.sleep(1)

标签: python, 多线程

非特殊说明,本博所有文章均为博主原创。

最新文章

发表评论