Scott Nimrod
Scott Nimrod

Reputation: 11570

Why do I have only one thread id when using Async.Parallel?

Why do I have only one processor id when using Async.Parallel?

Code:

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)

Output:

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 = ()

> 

Upvotes: 3

Views: 88

Answers (1)

scrwtp
scrwtp

Reputation: 13577

You're doing printfn from your main thread, outside your async workflow. Try this:

[for i in 1..10 -> 
    async {return sprintf "Processor Id: %d Value: %d" Thread.CurrentThread.ManagedThreadId i}] 
|> Async.Parallel
|> Async.RunSynchronously

Upvotes: 8

Related Questions