Udostępnij za pośrednictwem


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)

Parameters

  • computations
    Type: seq<Async<'T>>

    A sequence of distinct computations to be parallelized.

Return Value

A computation that returns an array of values from the sequence of input computations.

Remarks

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.

Example

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

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Control.Async Class (F#)

Microsoft.FSharp.Control Namespace (F#)