17 people like it.

Async.Parallel2 and Async.Parallel3

Async.Parallel2 and Async.Parallel3, for running three Async's in parallel as thread pool tasks. Alternative versions given which use Async.Parallel under the hood.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
module Async =
    /// Run two async's in parallel in the thread pool and return the results together, asynchronously
    let Parallel2 (p1, p2) = 
        async { let! job1 = Async.StartChild p1
                let! job2 = Async.StartChild p2
                let! res1 = job1
                let! res2 = job2
                return (res1, res2) }

    /// Run three async's in parallel in the thread pool and return the results together, asynchronously
    let Parallel3 (p1, p2, p3) = 
        async { let! job1 = Async.StartChild p1
                let! job2 = Async.StartChild p2
                let! job3 = Async.StartChild p3
                let! res1 = job1
                let! res2 = job2
                let! res3 = job3
                return (res1, res2, res3) }

    let private boxp p = async { let! res = p in return box res }

    /// Alternative version of Async.Parallel2
    let Parallel2b (p1:Async<'T1>, p2:Async<'T2>) : Async<'T1 * 'T2> = 
        async { let! results = Async.Parallel [| boxp p1; boxp p2 |]
                return (unbox results.[0],unbox results.[1]) }

    /// Alternative version of Async.Parallel3
    let Parallel3b (p1:Async<'T1>, p2:Async<'T2>, p3:Async<'T3>) : Async<'T1 * 'T2 * 'T3> = 
        let boxp p = async { let! res = p in return box res }
        async { let! results = Async.Parallel [| boxp p1; boxp p2; boxp p3 |]
                return (unbox results.[0], unbox results.[1], unbox results.[2]) }
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
val Parallel2 : p1:Async<'a> * p2:Async<'b> -> Async<'a * 'b>

Full name: Script.Async.Parallel2


 Run two async's in parallel in the thread pool and return the results together, asynchronously
val p1 : Async<'a>
val p2 : Async<'b>
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val job1 : Async<'a>
static member Async.StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
val job2 : Async<'b>
val res1 : 'a
val res2 : 'b
val Parallel3 : p1:Async<'a> * p2:Async<'b> * p3:Async<'c> -> Async<'a * 'b * 'c>

Full name: Script.Async.Parallel3


 Run three async's in parallel in the thread pool and return the results together, asynchronously
val p3 : Async<'c>
val job3 : Async<'c>
val res3 : 'c
val private boxp : p:Async<'a> -> Async<obj>

Full name: Script.Async.boxp
val p : Async<'a>
val res : 'a
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
val Parallel2b : p1:Async<'T1> * p2:Async<'T2> -> Async<'T1 * 'T2>

Full name: Script.Async.Parallel2b


 Alternative version of Async.Parallel2
val p1 : Async<'T1>
val p2 : Async<'T2>
val results : obj []
static member Async.Parallel : computations:seq<Async<'T>> -> Async<'T []>
val unbox : value:obj -> 'T

Full name: Microsoft.FSharp.Core.Operators.unbox
val Parallel3b : p1:Async<'T1> * p2:Async<'T2> * p3:Async<'T3> -> Async<'T1 * 'T2 * 'T3>

Full name: Script.Async.Parallel3b


 Alternative version of Async.Parallel3
val p3 : Async<'T3>
val boxp : (Async<'a> -> Async<obj>)

More information

Link:http://fssnip.net/ab
Posted:12 years ago
Author:Don Syme
Tags: async , parallel