日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Rust異步編程的可觀察調試工具:Await-Tree

來源: 責編: 時間:2024-02-05 17:21:08 244觀看
導讀Async Rust中的future可以任意組合或嵌套,以實現各種控制流。假設每個Future的執行都表示為一個節點,那么可以將異步任務的異步執行組織到一個邏輯樹中,該邏輯樹在Future的輪詢、完成和取消過程中不斷轉換。在本文中,我們

Async Rust中的future可以任意組合或嵌套,以實現各種控制流。假設每個Future的執行都表示為一個節點,那么可以將異步任務的異步執行組織到一個邏輯樹中,該邏輯樹在Future的輪詢、完成和取消過程中不斷轉換。VYB28資訊網——每日最新資訊28at.com

在本文中,我們將介紹Await-Tree,一個Async Rust的調試工具。它可以分析任務中的異步調用鏈和任務之間的依賴阻塞關系,以最小的運行時開銷顯著提高系統的可觀察性和可調試性。await-tree允許開發人員在運行時轉儲這個執行樹,每個Future的跨度由instrument_await注釋。VYB28資訊網——每日最新資訊28at.com

下面我們看一個基本示例:VYB28資訊網——每日最新資訊28at.com

在Cargo.toml文件中,加入以下依賴項:VYB28資訊網——每日最新資訊28at.com

[dependencies]await-tree = "0.1.2"futures = "0.3.30"tokio = {version = "1.35.1", features = ["full"]}

VYB28資訊網——每日最新資訊28at.com

代碼如下:VYB28資訊網——每日最新資訊28at.com

use std::time::Duration;use await_tree::{Config, InstrumentAwait, Registry};use futures::future::{join, pending};use tokio::time::sleep;async fn bar(i: i32) {    // `&'static str` span    baz(i).instrument_await("baz in bar").await}async fn baz(i: i32) {    // runtime `String` span is also supported    pending()        .instrument_await(format!("pending in baz {i}"))        .await}async fn foo() {    // spans of joined futures will be siblings in the tree    join(        bar(3).instrument_await("bar"),        baz(2).instrument_await("baz"),    )    .await;}#[tokio::main]async fn main() {    let mut registry = Registry::new(Config::default());    let root = registry.register((), "foo");    tokio::spawn(root.instrument(foo()));    sleep(Duration::from_secs(1)).await;    let tree = registry.get(&()).unwrap().to_string();    println!("{tree}");}

VYB28資訊網——每日最新資訊28at.com

執行cargo run,結果如下:VYB28資訊網——每日最新資訊28at.com

foo [1.002s]  baz [1.002s]    pending in baz 2 [1.002s]  bar [1.002s]    baz in bar [1.002s]      pending in baz 3 [1.002s]

VYB28資訊網——每日最新資訊28at.com

在代碼中,我們有一些簡單的async函數嵌套調用和使用join并發執行。與通常的代碼不同,我們在希望跟蹤的每個關鍵future后面添加.instrument_await,并為其指定名稱。此名稱可以是靜態字符串常量,也可以包含其他運行時信息。VYB28資訊網——每日最新資訊28at.com

我們再看另外一個例子:VYB28資訊網——每日最新資訊28at.com

use std::time::Duration;use await_tree::{Config, InstrumentAwait, Registry};use futures::channel::oneshot::{self, Receiver};use futures::future::{pending, select};use futures::FutureExt;use tokio::time::sleep;async fn work(rx: Receiver<()>) {    let mut fut = pending().instrument_await("fut");    let _ = select(        sleep(Duration::from_millis(500))            .instrument_await("sleep")            .boxed(),        &mut fut,    )    .instrument_await("select")    .await;    // 等待信號繼續    rx.instrument_await("rx").await.unwrap();    fut.await}#[tokio::main]async fn main() {    let mut registry = Registry::new(Config::default());    let root = registry.register((), "work");    let (tx, rx) = oneshot::channel();    tokio::spawn(root.instrument(work(rx)));    sleep(Duration::from_millis(100)).await;    let tree = registry.get(&()).unwrap().to_string();    println!("{tree}");    sleep(Duration::from_secs(1)).await;    let tree = registry.get(&()).unwrap().to_string();    println!("{tree}");    tx.send(()).unwrap();    sleep(Duration::from_secs(1)).await;    let tree = registry.get(&()).unwrap().to_string();    println!("{tree}");}

VYB28資訊網——每日最新資訊28at.com

結果如下:VYB28資訊網——每日最新資訊28at.com

work [101.181ms]  select [101.066ms]    fut [101.044ms]    sleep [101.044ms]work [1.103s]  rx [601.779ms][Detached 4]  fut [1.103s]work [2.105s]  fut [2.105s]

VYB28資訊網——每日最新資訊28at.com

這個例子展示了如何從樹中分離并重新掛載一個span。VYB28資訊網——每日最新資訊28at.com

VYB28資訊網——每日最新資訊28at.com

VYB28資訊網——每日最新資訊28at.com

總結

在本文中,我們介紹了await- tree作為Async Rust中可觀察性的強大工具。await- tree是為Async Rust原生設計的回溯工具,它允許開發者實時觀察每個異步任務的執行狀態,并分析不同future或任務之間的依賴阻塞關系。VYB28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-74193-0.htmlRust異步編程的可觀察調試工具:Await-Tree

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: .NET中使用BootstrapBlazor組件庫Table實操篇

下一篇: 創建線程的幾種方式?你知道嗎?

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 邵东县| 沁源县| 葫芦岛市| 竹山县| 开江县| 湖北省| 沐川县| 平阴县| 汉川市| 修水县| 麻城市| 铜山县| 景谷| 常德市| 齐齐哈尔市| 凉城县| 汉源县| 锦屏县| 金沙县| 屯门区| 万山特区| 莎车县| 于田县| 贵德县| 项城市| 兴安盟| 双柏县| 竹北市| 陕西省| 赣州市| 鄄城县| 巴林左旗| 易门县| 达孜县| 斗六市| 卓尼县| 丹阳市| 嘉义市| 宁安市| 巧家县| 沾化县|