为什么我在使用 Async.Parallel 时只有一个处理器 ID?
代码:
open System
open System.Threading
[for i in 1..10 -> async {return i}] |> Async.Parallel
|> Async.RunSynchronously
|> Seq.iter (fun i -> printfn "Processor Id: %d Value: %d" Thread.CurrentThread.ManagedThreadId i)
输出:
Processor Id: 1 Value: 1 Processor Id: 1 Value: 2 Processor Id: 1 Value: 3 Processor Id: 1 Value: 4 Processor Id: 1 Value: 5 Processor Id: 1 Value: 6 Processor Id: 1 Value: 7 Processor Id: 1 Value: 8 Processor Id: 1 Value: 9 Processor Id: 1 Value: 10
val it : unit = ()
>
最佳答案
您正在从您的主线程执行 printfn
,在您的异步工作流程之外。试试这个:
[for i in 1..10 ->
async {return sprintf "Processor Id: %d Value: %d" Thread.CurrentThread.ManagedThreadId i}]
|> Async.Parallel
|> Async.RunSynchronously
关于multithreading - 为什么我在使用 Async.Parallel 时只有一个线程 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37509149/