Part VII: 非同期プログラミング¶
概要¶
本章では、F# の Async ワークフローを詳しく学びます。
Async ワークフローの基本¶
// async { } 内で非同期計算を記述
let computation : Async<int> =
async {
printfn "Starting..."
do! Async.Sleep(1000)
printfn "Done!"
return 42
}
let! と do!¶
let example : Async<int> =
async {
// let! : 値を取り出す
let! result = someAsyncComputation()
// do! : 値を無視
do! Async.Sleep(100)
return result * 2
}
合成¶
// 順次実行
let sequential : Async<int> =
async {
let! a = computeA()
let! b = computeB()
return a + b
}
// 並列実行
let parallel : Async<int[]> =
[computeA(); computeB(); computeC()]
|> Async.Parallel
エラーハンドリング¶
let safeComputation : Async<Result<int, string>> =
async {
try
let! result = riskyComputation()
return Ok result
with
| ex -> return Error ex.Message
}
キャンセル¶
let cancellableComputation (ct: CancellationToken) : Async<unit> =
async {
for i in 1 .. 100 do
ct.ThrowIfCancellationRequested()
do! Async.Sleep(100)
printfn $"Step {i}"
}
// 使用例
let cts = new CancellationTokenSource()
Async.Start(cancellableComputation cts.Token)
// キャンセル
cts.Cancel()
Task との相互運用¶
// Task -> Async
let asyncFromTask : Async<string> =
someTask |> Async.AwaitTask
// Async -> Task
let taskFromAsync : Task<string> =
someAsync |> Async.StartAsTask
次のステップ¶
Part VIII では、MapReduce パターンと並列配列を学びます。