使用 async 和 await
async/await support is currently being integrated in PyO3. See the dedicated documentation
若你使用的 Python 函式庫包含 async 函式,或希望為 async Rust 函式庫提供 Python 綁定,pyo3-async-runtimes 很可能就是你需要的工具。它提供 Python 與 Rust 之間 async 函式的轉換,並以對 tokio 與 async-std 等常見 Rust runtime 的一級支援為設計目標。此外,所有 async Python 程式碼都在預設的 asyncio 事件迴圈上執行,因此 pyo3-async-runtimes 應可與既有 Python 函式庫順利運作。
補充資訊
- 在
pyo3-async-runtimes中管理事件迴圈參照可能較棘手。請參考 API 文件中的 Event Loop References,以更了解此函式庫如何管理事件迴圈參照。 - 測試
pyo3-async-runtimes函式庫與應用程式需要自訂測試框架,因為 Python 需要掌控主執行緒。測試指南請見testing模組的 API 文件。