Python - multithread

#!/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, **kargs): self.tasks.put((func, args, kargs)) # 放入tasks queue def wait_tasks_completion(self): self.tasks.join() # 等待tasks queue的所有task都完成 if __name__ == '__main__': class A(object): def __init__(self): self.a = 0; def addA(self): self.a+=1; print self.a pool = ThreadPoolManager(10) # 決定要建立10個workers  objectA = A()
for i in range(10):
pool.add_task(objectA.addA)
pool.wait_tasks_completion() # 等待所有task完成
 =========執行結果========= 12 34 5 6 7 8 9 10 會發現印出的結果很混亂 因此可以加入lock讓結果是有順序的印出 #!/usr/bin/python #-*- coding:utf-8 -*- ''' @author: Duncan ''' from Queue import Queue from threading import Thread,Lock class ThreadWorker(Thread): def __init__(self, name, tasks,lock): Thread.__init__(self) self.tasks = tasks # tasks queue self.daemon = True # 需在start之前,具有和main thread一同終止的特性,預設是False self.start() self.name = name # 可以分別設定thread的name self.lock = lock def run(self): while True: func, args, kargs = self.tasks.get() # 從tasks queue取出task try: self.lock.acquire() func(*args, **kargs) # 執行function self.lock.release() except Exception as e: print e self.tasks.task_done() class ThreadPoolManager: def __init__(self, number_threads): self.lockObject = Lock() self.tasks = Queue() # 建立沒有限制長度的queue for _ in range(number_threads): ThreadWorker(_,self.tasks,self.lockObject) # 啟動根據num_threads數量的task任務 def add_task(self, func, *args, **kargs): self.tasks.put((func, args, kargs)) # 放入tasks queue def wait_tasks_completion(self): self.tasks.join() # 等待tasks queue的所有task都完成 if __name__ == '__main__': class A(object): def __init__(self): self.a = 0; def addA(self): self.a+=1; print self.a pool = ThreadPoolManager(10) # 決定要建立10個workers objectA = A() for i in range(10): pool.add_task(objectA.addA) pool.wait_tasks_completion() # 等待所有task完成 =========執行結果========= 1 2 3 4 5 6 7 8 9 10

==參考來源==

留言

這個網誌中的熱門文章

Python - 計算特定目錄底下的檔案以及目錄數量

PHP - 產生qrcode

devstack安裝all in one openstack(pike)