Skip to content

Part VI: ノンブロッキング I/O

概要

本章では、Rust の async/await を使用したノンブロッキング I/O を学びます。


ブロッキング vs ノンブロッキング

ブロッキング I/O

use std::fs;

// スレッドは I/O 完了まで待機
let content = fs::read_to_string("file.txt").unwrap();

ノンブロッキング I/O (async)

use tokio::fs;

async fn read_file_async(path: &str) -> String {
    fs::read_to_string(path).await.unwrap()
}

Tokio の基本

use tokio;

#[tokio::main]
async fn main() {
    let result = fetch_data("https://example.com").await;
    println!("{}", result);
}

async fn fetch_data(url: &str) -> String {
    // 非同期 HTTP リクエスト
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    format!("Data from {}", url)
}

並行実行

use tokio;

#[tokio::main]
async fn main() {
    // 並行実行
    let (r1, r2, r3) = tokio::join!(
        fetch_data("url1"),
        fetch_data("url2"),
        fetch_data("url3"),
    );

    println!("{}, {}, {}", r1, r2, r3);
}

メリット

項目 ブロッキング async
スレッド消費 待機中も占有 解放
スケーラビリティ 低い 高い
コード複雑さ 低い やや高い

次のステップ

Part VII では、非同期プログラミングの詳細を学びます。