#!/usr/bin/python #-*- coding:utf-8 -*- ''' @author: Duncan ''' from Queue import Queue from threading import Thread class ThreadWorker(Thread): def __init__(self, name, tasks): Thread.__init__(self) self.tasks = tasks # tasks queue self.daemon = True # 需在start之前,具有和main thread一同終止的特性,預設是False self.start() self.name = name # 可以分別設定thread的name def run(self): while True: func, args, kargs = self.tasks.get() # 從tasks queue取出task try: func(*args, **kargs) # 執行function except Exception as e: print e self.tasks.task_done() class ThreadPoolManager: def __init__(self, number_threads): self.tasks = Queue( ) # 建立沒有限制長度的queue for _ in range( number_threads ): ThreadWorker(_,self.tasks) # 啟動根據 num_threads數量的task任務 def add_task(self, func, *args, **ka...