Python 多线程运行多个job的框架

在做机器学习很多时候浪费时间到了数据处理上面, 现在实现了一个python多线程的程序,希望这个框架能够用于数据处理阶段多线程的运行

#coding=utf-8
import threading
from time import ctime,sleep


tasklist = [1,2,3,4,5,6,7,8,9,10]
tasklist = tasklist*3
thread_num = 5

def runtask(taskid):
    print("I was runing task %d" %(taskid))
    sleep(5)



thread_list = []

if __name__ == '__main__':

    task_num = len(tasklist)

    task_index = 0
    thread_count = 0
    print ctime()
    while True:
        if task_index < task_num and thread_count < thread_num:
            t = threading.Thread(target= runtask, args=(tasklist[task_index],))
            t.setDaemon(True)
            t.start()
            thread_list.append(t)
            task_index += 1
            thread_count += 1
        for t in thread_list:
            if not t.isAlive():
                thread_list.remove(t)
                thread_count -= 1
        if task_index == task_num and thread_list == []:
            break
    print ctime()
    print("Finsh all threads!")

你可能感兴趣的:(python)