Under the cover, the operation Async.Paralleluses the standard .NET thread pool. This way you can configure the thread pool, but this is probably not a good idea (you should not block threads in the thread pool).
, , , F #. concurrency - , , , ( ), :
type ThrottlingMessage =
| Enqueue of Async<unit>
| Completed
let throttlingAgent limit = MailboxProcessor.Start(fun inbox -> async {
let queue = System.Collections.Generic.Queue<_>()
let running = ref 0
while true do
let! msg = inbox.Receive()
match msg with
| Completed -> decr running
| Enqueue w -> queue.Enqueue(w)
while running.Value < limit && queue.Count > 0 do
let work = queue.Dequeue()
incr running
do!
async { do! work
inbox.Post(Completed) }
|> Async.StartChild
|> Async.Ignore })
, , Enqueue, :
let w = throttlingAgent 5
for i in 0 .. 20 do
async { printfn "Starting %d" i
do! Async.Sleep(1000)
printfn "Done %d" i }
|> Enqueue
|> w.Post
, , , - ( , Completed, , , async "" ).