Async.Parallel<'T> Method (F#)
Creates an asynchronous computation that executes all the given asynchronous computations, initially queueing each as work items and using a fork/join pattern.
Namespace/Module Path: Microsoft.FSharp.Control
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature: static member Parallel : seq<Async<'T>> -> Async<'T []> // Usage: Async.Parallel (computations)
If all child computations succeed, an array of results is passed to the success continuation. If any child computation raises an exception, then the overall computation will trigger an exception, and cancel the others. The overall computation will respond to cancellation while executing the child computations. If cancelled, the computation will cancel any remaining child computations but will still wait for the other child computations to complete.
The following code example shows how to use Async.Parallel to run computations that write to a number of files asynchronously.
let bufferData (number:int) = [| for count in 1 .. 1000 -> byte (count % 256) |] |> Array.permute (fun index -> index) let writeFiles bufferData = Seq.init 1000 (fun num -> bufferData num) |> Seq.mapi (fun num value -> async { let fileName = "file" + num.ToString() + ".dat" use outputFile = System.IO.File.Create(fileName) do! outputFile.AsyncWrite(value) }) |> Async.Parallel |> Async.Ignore writeFiles bufferData |> Async.Start