線程 |
線程 |
Threading用於提供線程相關的操做。線程是應用程序中工做的最小單元,它被包含在進程之中,是進程中的實際運做單位。一條線程指的是進程中一個單一順序的控制流,一個進程中能夠併發多個線程,每條線程並行執行不一樣的任務。python
threading 模塊創建在 _thread 模塊之上。thread 模塊以低級、原始的方式來處理和控制線程,而 threading 模塊經過對 thread 進行二次封裝,提供了更方便的 api 來處理線程。git
1程序員 2github 3編程 4api 5安全 6網絡 7數據結構 8多線程 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
thread方法:
線程鎖
threading.RLock & threading.Lock
咱們使用線程對數據進行操做的時候,若是多個線程同時修改某個數據,可能會出現不可預料的結果,爲了保證數據的準確性,引入了鎖的概念。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
threading.RLock和threading.Lock 的區別
RLock容許在同一線程中被屢次acquire。而Lock卻不容許這種狀況。 若是使用RLock,那麼acquire和release必須成對出現,即調用了n次acquire,必須調用n次的release才能真正釋放所佔用的鎖。
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 |
|
threading.Event
Event是線程間通訊最間的機制之一:一個線程發送一個event信號,其餘的線程則等待這個信號。用於主線程控制其餘線程的執行。 Events 管理一個flag,這個flag可使用set()設置成True或者使用clear()重置爲False,wait()則用於阻塞,在flag爲True以前。flag默認爲False。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
當線程執行的時候,若是flag爲False,則線程會阻塞,當flag爲True的時候,線程不會阻塞。它提供了本地和遠程的併發性。
threading.Condition
Python提供的Condition對象提供了對複雜線程同步問題的支持。Condition被稱爲條件變量,除了提供與Lock相似的acquire和release方法外,還提供了wait和notify方法。線程首先acquire一個條件變量,而後判斷一些條件。若是條件不知足則wait;若是條件知足,進行一些處理改變條件後,經過notify方法通知其餘線程,其餘處於wait狀態的線程接到通知後會從新判斷條件。不斷的重複這一過程,從而解決複雜的同步問題。
在典型的設計風格里,利用condition變量用鎖去通許訪問一些共享狀態,線程在獲取到它想獲得的狀態前,會反覆調用wait()。修改狀態的線程在他們狀態改變時調用 notify() or notify_all(),用這種方式,線程會盡量的獲取到想要的一個等待者狀態。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
queue 隊列
適用於多線程編程的先進先出數據結構,能夠用來安全的傳遞多線程信息。
queue 方法:
生產者消費者模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
自定義線程池:
# 自定義線程池(一) import queue import threading import time class TreadPool: def __init__(self, max_num=20): self.queue = queue.Queue(max_num) for i in range(max_num): self.queue.put(threading.Thread) def get_thread(self): return self.queue.get() def add_thread(self): self.queue.put(threading.Thread) def func(pool, n): time.sleep(1) print(n) pool.add_thread() p = TreadPool(10) for i in range(1, 100): thread = p.get_thread() t = thread(target=func, args=(p, i,)) t.start()
# 線程池(二) import queue import threading import contextlib import time StopEvent = object() class Threadpool: def __init__(self, max_num=10): self.q = queue.Queue() self.max_num = max_num self.terminal = False self.generate_list = [] # 以建立線程列表 self.free_list = [] # 以建立的線程空閒列表 def run(self, func, args, callback=None): """ 線程池執行一個任務 :param func: 任務函數 :param args: 任務函數所需參數 :param callback: 任務執行失敗或成功後執行的回調函數,回調函數有兩個參數一、任務函數執行狀態;二、任務函數返回值(默認爲None,即:不執行回調函數) :return: 若是線程池已經終止,則返回True不然None """ if len(self.free_list) == 0 and len(self.generate_list) < self.max_num: self.generate_thread() w = (func, args, callback,) self.q.put(w) def generate_thread(self): """ 建立一個線程 """ t = threading.Thread(target=self.call) t.start() def call(self): """ 循環去獲取任務函數並執行任務函數 """ current_thread = threading.currentThread # 當前線程 self.generate_list.append(current_thread) event = self.q.get() while event != StopEvent: func, arguments, callback = event try: result = func(*arguments) status = True except Exception as e: status = False result = e if callback is not None: try: callback(status, result) except Exception as e: pass if self.terminal: event = StopEvent else: with self.worker_state(self.free_list, current_thread): event = self.q.get() # self.free_list.append(current_thread) # event = self.q.get() # self.free_list.remove(current_thread) else: self.generate_list.remove(current_thread) def close(self): """ 執行完全部的任務後,全部線程中止 """ num = len(self.generate_list) while num: self.q.put(StopEvent) num -= 1 def terminate(self): """ 不管是否還有任務,終止線程 """ self.terminal = True while self.generate_list: self.q.put(StopEvent) self.q.empty() # 清空隊列 @contextlib.contextmanager # with上下文管理 def worker_state(self, frelist, val): """ 用於記錄線程中正在等待的線程數 """ frelist.append(val) try: yield finally: frelist.remove(val) def work(i): time.sleep(1) print(i) pool = Threadpool() for item in range(50): pool.run(func=work, args=(item,)) pool.close() # pool.terminate() 自定義線程池(二)
進程 |
1 2 3 4 5 6 7 8 9 10 |
|
注意:因爲進程之間的數據須要各自持有一份,因此建立進程須要的很是大的開銷。
數據共享
不一樣進程間內存是不共享的,要想實現兩個進程間的數據交換,能夠用如下方法:
Shared memory
數據能夠用Value或Array存儲在一個共享內存地圖裏,以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
建立num和arr時,「d」和「i」參數由Array模塊使用的typecodes建立:「d」表示一個雙精度的浮點數,「i」表示一個有符號的整數,這些共享對象將被線程安全的處理。
‘c’: ctypes.c_char ‘u’: ctypes.c_wchar ‘b’: ctypes.c_byte ‘B’: ctypes.c_ubyte ‘h’: ctypes.c_short ‘H’: ctypes.c_ushort ‘i’: ctypes.c_int ‘I’: ctypes.c_uint ‘l’: ctypes.c_long, ‘L’: ctypes.c_ulong ‘f’: ctypes.c_float ‘d’: ctypes.c_double
1 2 3 4 5 6 7 8 9 10 11 |
|
Server process
由Manager()返回的manager提供list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Barrier, Queue, Value and Array類型的支持。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Server process manager比 shared memory 更靈活,由於它能夠支持任意的對象類型。另外,一個單獨的manager能夠經過進程在網絡上不一樣的計算機之間共享,不過他比shared memory要慢。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
當建立進程時(非使用時),共享數據會被拿到子進程中,當進程中執行完畢後,再賦值給原值。
#!/usr/bin/env python # -*- coding:utf-8 -*- from multiprocessing import Process, Array, RLock def Foo(lock,temp,i): """ 將第0個數加100 """ lock.acquire() temp[0] = 100+i for item in temp: print i,'----->',item lock.release() lock = RLock() temp = Array('i', [11, 22, 33, 44]) for i in range(20): p = Process(target=Foo,args=(lock,temp,i,)) p.start() 進程鎖實例
進程池內部維護一個進程序列,當使用時,則去進程池中獲取一個進程,若是進程池序列中沒有可供使用的進進程,那麼程序就會等待,直到進程池中有可用進程爲止。
方法:
apply(func[, args[, kwds]]) :使用arg和kwds參數調用func函數,結果返回前會一直阻塞,因爲這個緣由,apply_async()更適合併發執行,另外,func函數僅被pool中的一個進程運行。
apply_async(func[, args[, kwds[, callback[, error_callback]]]]) : apply()方法的一個變體,會返回一個結果對象。若是callback被指定,那麼callback能夠接收一個參數而後被調用,當結果準備好回調時會調用callback,調用失敗時,則用error_callback替換callback。 Callbacks應被當即完成,不然處理結果的線程會被阻塞。
close() : 阻止更多的任務提交到pool,待任務完成後,工做進程會退出。
terminate() : 無論任務是否完成,當即中止工做進程。在對pool對象進程垃圾回收的時候,會當即調用terminate()。
join() : wait工做線程的退出,在調用join()前,必須調用close() or terminate()。這樣是由於被終止的進程須要被父進程調用wait(join等價與wait),不然進程會成爲殭屍進程
進程池中有兩個方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
官方示例
from multiprocessing import Pool, TimeoutError import time import os def f(x): return x*x if __name__ == '__main__': # 建立4個進程 with Pool(processes=4) as pool: # 打印 "[0, 1, 4,..., 81]" print(pool.map(f, range(10))) # 使用任意順序輸出相同的數字, for i in pool.imap_unordered(f, range(10)): print(i) # 異步執行"f(20)" res = pool.apply_async(f, (20,)) # 只運行一個進程 print(res.get(timeout=1)) # 輸出 "400" # 異步執行 "os.getpid()" res = pool.apply_async(os.getpid, ()) # 只運行一個進程 print(res.get(timeout=1)) # 輸出進程的 PID # 運行多個異步執行可能會使用多個進程 multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)] print([res.get(timeout=1) for res in multiple_results]) # 是一個進程睡10秒 res = pool.apply_async(time.sleep, (10,)) try: print(res.get(timeout=1)) except TimeoutError: print("發現一個 multiprocessing.TimeoutError異常") print("目前,池中還有其餘的工做") # 退出with塊中已經中止的池 print("Now the pool is closed and no longer available") 官方示例
協程 |
協程又叫微線程,從技術的角度來講,「協程就是你能夠暫停執行的函數」。若是你把它理解成「就像生成器同樣」,那麼你就想對了。 線程和進程的操做是由程序觸發系統接口,最後的執行者是系統;協程的操做則是程序員。
協程存在的意義:對於多線程應用,CPU經過切片的方式來切換線程間的執行,線程切換時須要耗時(保存狀態,下次繼續)。協程,則只使用一個線程,在一個線程中規定某個代碼塊執行順序。
協程的適用場景:當程序中存在大量不須要CPU的操做時(IO),適用於協程。
1 2 3 4 5 |
|
greenlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
gevent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
遇到IO操做自動切換
# 遇到IO自動切換 from gevent import monkey monkey.patch_all() import gevent import requests def f(url): print("FET: %s" % url) resp = requests.get(url) data = len(resp.text) print(url, data) gevent.joinall([ gevent.spawn(f, 'https://www.python.org/'), gevent.spawn(f, 'https://www.yahoo.com/'), gevent.spawn(f, 'https://github.com/'), ]) 遇到IO操做自動切換