
- Python 基礎
- Python 首頁
- Python 概述
- Python 歷史
- Python 特性
- Python vs C++
- Python - Hello World 程式
- Python 應用領域
- Python 直譯器
- Python 環境設定
- Python 虛擬環境
- Python 基本語法
- Python 變數
- Python 資料型別
- Python 型別轉換
- Python Unicode 系統
- Python 字面量
- Python 運算子
- Python 算術運算子
- Python 比較運算子
- Python 賦值運算子
- Python 邏輯運算子
- Python 位運算子
- Python 成員運算子
- Python 身份運算子
- Python 運算子優先順序
- Python 註釋
- Python 使用者輸入
- Python 數字
- Python 布林值
- Python 控制語句
- Python 控制流
- Python 決策
- Python if 語句
- Python if else
- Python 巢狀 if
- Python match-case 語句
- Python 迴圈
- Python for 迴圈
- Python for-else 迴圈
- Python while 迴圈
- Python break 語句
- Python continue 語句
- Python pass 語句
- Python 巢狀迴圈
- Python 函式與模組
- Python 函式
- Python 預設引數
- Python 關鍵字引數
- Python 僅限關鍵字引數
- Python 位置引數
- Python 僅限位置引數
- Python 可變引數
- Python 變數作用域
- Python 函式註解
- Python 模組
- Python 內建函式
- Python 字串
- Python 字串
- Python 字串切片
- Python 修改字串
- Python 字串連線
- Python 字串格式化
- Python 跳脫字元
- Python 字串方法
- Python 字串練習
- Python 列表
- Python 列表
- Python 訪問列表元素
- Python 修改列表元素
- Python 新增列表元素
- Python 刪除列表元素
- Python 遍歷列表
- Python 列表推導式
- Python 排序列表
- Python 複製列表
- Python 合併列表
- Python 列表方法
- Python 列表練習
- Python 元組
- Python 元組
- Python 訪問元組元素
- Python 更新元組
- Python 解包元組
- Python 遍歷元組
- Python 合併元組
- Python 元組方法
- Python 元組練習
- Python 集合
- Python 集合
- Python 訪問集合元素
- Python 新增集合元素
- Python 刪除集合元素
- Python 遍歷集合
- Python 合併集合
- Python 複製集合
- Python 集合運算子
- Python 集合方法
- Python 集合練習
- Python 字典
- Python 字典
- Python 訪問字典元素
- Python 修改字典元素
- Python 新增字典元素
- Python 刪除字典元素
- Python 字典檢視物件
- Python 遍歷字典
- Python 複製字典
- Python 巢狀字典
- Python 字典方法
- Python 字典練習
- Python 陣列
- Python 陣列
- Python 訪問陣列元素
- Python 新增陣列元素
- Python 刪除陣列元素
- Python 遍歷陣列
- Python 複製陣列
- Python 反轉陣列
- Python 排序陣列
- Python 合併陣列
- Python 陣列方法
- Python 陣列練習
- Python 檔案處理
- Python 檔案處理
- Python 寫入檔案
- Python 讀取檔案
- Python 重新命名和刪除檔案
- Python 目錄
- Python 檔案方法
- Python OS 檔案/目錄方法
- Python OS 路徑方法
- 面向物件程式設計
- Python OOPs 概念
- Python 類與物件
- Python 類屬性
- Python 類方法
- Python 靜態方法
- Python 建構函式
- Python 訪問修飾符
- Python 繼承
- Python 多型
- Python 方法重寫
- Python 方法過載
- Python 動態繫結
- Python 動態型別
- Python 抽象
- Python 封裝
- Python 介面
- Python 包
- Python 內部類
- Python 匿名類和物件
- Python 單例類
- Python 包裝類
- Python 列舉
- Python 反射
- Python 錯誤與異常
- Python 語法錯誤
- Python 異常
- Python try-except 塊
- Python try-finally 塊
- Python 丟擲異常
- Python 異常鏈
- Python 巢狀 try 塊
- Python 使用者自定義異常
- Python 日誌記錄
- Python 斷言
- Python 內建異常
- Python 多執行緒
- Python 多執行緒
- Python 執行緒生命週期
- Python 建立執行緒
- Python 啟動執行緒
- Python 加入執行緒
- Python 執行緒命名
- Python 執行緒排程
- Python 執行緒池
- Python 主執行緒
- Python 執行緒優先順序
- Python 守護執行緒
- Python 執行緒同步
- Python 同步
- Python 執行緒間通訊
- Python 執行緒死鎖
- Python 中斷執行緒
- Python 網路程式設計
- Python 網路程式設計
- Python 套接字程式設計
- Python URL 處理
- Python 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python 日期與時間
- Python 數學
- Python 迭代器
- Python 生成器
- Python 閉包
- Python 裝飾器
- Python 遞迴
- Python 正則表示式
- Python PIP
- Python 資料庫訪問
- Python 弱引用
- Python 序列化
- Python 模板
- Python 輸出格式化
- Python 效能測量
- Python 資料壓縮
- Python CGI 程式設計
- Python XML 處理
- Python GUI 程式設計
- Python 命令列引數
- Python 文件字串
- Python JSON
- Python 傳送郵件
- Python 擴充套件
- Python 工具/實用程式
- Python GUIs
- Python 高階概念
- Python 抽象基類
- Python 自定義異常
- Python 高階函式
- Python 物件內部
- Python 記憶體管理
- Python 元類
- Python 使用元類進行超程式設計
- Python 模擬和存根
- Python 猴子補丁
- Python 訊號處理
- Python 型別提示
- Python 自動化教程
- Python Humanize 包
- Python 上下文管理器
- Python 協程
- Python 描述符
- Python 診斷和修復記憶體洩漏
- Python 不可變資料結構
- Python 有用資源
- Python 問答
- Python 線上測驗
- Python 快速指南
- Python 參考
- Python 速查表
- Python 專案
- Python 有用資源
- Python 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python 協程
Python **協程**是程式設計中的一個基本概念,它擴充套件了傳統函式的功能。它們在非同步程式設計和複雜資料處理流水線中特別有用。
**協程**是函式和生成器概念的擴充套件。它們旨在執行協作式多工處理和管理非同步操作。
傳統函式(即子程式)只有一個入口點和一個出口點,而協程可以在多個點暫停和恢復執行,這使得它們非常靈活。
協程的關鍵特性
以下是 Python 中協程的關鍵特性:
- **多個入口點:**協程不像傳統函式那樣只受限於單個入口點。它們可以在遇到 `yield` 語句時暫停執行,並在稍後恢復。這允許協程處理涉及等待或處理非同步資料的複雜工作流程。
- **沒有中央協調器:**傳統函式(即子程式)通常由主函式協調,但協程更獨立地執行。它們可以以流水線的方式相互互動,資料流經一系列協程,每個協程執行不同的任務。
- **協作式多工處理:**協程支援協作式多工處理。這意味著它不是依賴於作業系統或執行時在任務之間切換,而是由程式設計師控制協程何時讓出和恢復,從而允許對執行流程進行更細粒度的控制。
子程式與協程
**子程式**是具有單個入口點且沒有暫停或恢復執行的固有機制的傳統函式。它們按定義的順序呼叫,並處理具有直接控制流的任務。
**協程**是具有多個入口點的更高階函式,可以暫停和恢復執行。它們對於需要非同步執行、複雜控制流和資料流水線的任務很有用。它們透過允許程式設計師控制任務之間何時切換執行來支援協作式多工處理。
下表有助於理解子程式和協程之間的關鍵區別和相似之處,使我們更容易掌握它們在程式設計中的各自作用和功能。
標準 | 子程式 | 協程 |
---|---|---|
定義 | 執行任務的一系列指令。 | 可以暫停和恢復執行的子程式的泛化。 |
入口點 | 單個入口點。 | 多個入口點;可以暫停和恢復執行。 |
執行控制 | 由主函式或控制結構呼叫。 | 可以暫停執行並在以後恢復,程式設計師控制切換。 |
用途 | 執行特定任務或計算。 | 管理非同步操作、協作式多工處理和複雜工作流程。 |
呼叫機制 | 通常由主函式或其他子程式呼叫。 | 使用`next()`、`send()`和`close()`方法呼叫和控制。 |
資料處理 | 沒有內建的資料交換機制;通常使用引數和返回值。 | 可以使用帶有`send()`的`yield`接收和處理資料。 |
狀態管理 | 沒有固有的機制來維護呼叫之間的狀態。 | 在掛起之間維護執行狀態,並可以從中斷處恢復。 |
用法 | 這些用於將程式碼模組化成可管理的塊。 | 這些用於非同步程式設計、管理資料管道和協作式多工處理。 |
併發 | 並非天生設計用於併發執行;通常用於順序程式設計。 | 支援協作式多工處理,並可以與非同步任務一起工作。 |
示例用法 | 輔助函式,實用函式。 | 資料管道,非同步任務,協作式多工處理。 |
控制流 | 執行遵循程式碼中的線性路徑。 | 執行可以根據yield點在協程之間來回跳轉。 |
協程的執行
協程由__next__()方法啟動,該方法啟動協程並將執行推進到第一個yield語句。然後,協程等待向其傳送值。send()方法用於向協程傳送值,協程可以處理這些值並可能產生結果。
基本協程示例
協程使用yield語句,該語句可以傳送和接收值。與生成器不同,生成器產生用於迭代的值,而協程通常使用yield接收輸入並根據該輸入執行操作。以下是Python協程的基本示例:
def print_name(prefix): print(f"Searching prefix: {prefix}") while True: name = (yield) if prefix in name: print(name) # Instantiate the coroutine corou = print_name("Welcome to") # Start the coroutine corou.__next__() # Send values to the coroutine corou.send("Tutorialspoint") corou.send("Welcome to Tutorialspoint")
輸出
Searching prefix: Welcome to Welcome to Tutorialspoint
關閉協程
協程可以無限期執行,因此在不再需要它們時正確關閉它們非常重要。close()方法終止協程並處理清理工作。如果我們嘗試向已關閉的協程傳送資料,它將引發StopIteration異常。
示例
以下是 Python 中關閉協程的示例:
def print_name(prefix): print(f"Searching prefix: {prefix}") try: while True: name = (yield) if prefix in name: print(name) except GeneratorExit: print("Closing coroutine!!") # Instantiate and start the coroutine corou = print_name("Come") corou.__next__() # Send values to the coroutine corou.send("Come back Thank You") corou.send("Thank you") # Close the coroutine corou.close()
輸出
Searching prefix: Come Come back Thank You Closing coroutine!!
將協程連結起來形成管道
協程可以連結在一起形成處理管道,允許資料流經一系列階段。這對於分階段處理資料序列特別有用,其中每個階段執行特定任務。
示例
以下示例顯示了將協程連結起來形成管道的過程:
def producer(sentence, next_coroutine): ''' Splits the input sentence into tokens and sends them to the next coroutine. ''' tokens = sentence.split(" ") for token in tokens: next_coroutine.send(token) next_coroutine.close() def pattern_filter(pattern="ing", next_coroutine=None): ''' Filters tokens based on the specified pattern and sends matching tokens to the next coroutine. ''' print(f"Searching for {pattern}") try: while True: token = (yield) if pattern in token: next_coroutine.send(token) except GeneratorExit: print("Done with filtering!!") next_coroutine.close() def print_token(): ''' Receives tokens and prints them. ''' print("I'm the sink, I'll print tokens") try: while True: token = (yield) print(token) except GeneratorExit: print("Done with printing!") # Setting up the pipeline pt = print_token() pt.__next__() pf = pattern_filter(next_coroutine=pt) pf.__next__() sentence = "Tutorialspoint is welcoming you to learn and succeed in Career!!!" producer(sentence, pf)
輸出
I'm the sink, I'll print tokens Searching for ing welcoming Done with filtering!! Done with printing!