.net - Solution for common async pattern: Start many, await first, cancel the rest -
i'm looking pattern:
let startmanyawaitfirstcancelrest (n:async<'t> list) : async<'t> = // start n asyncs, // return result of first finish , // cancel rest.
something this?
open system open system.threading open system.threading.tasks let run (asyncs: list<async<'t>>): async<'t> = let cts = new cancellationtokensource() let tasks = asyncs |> list.map (fun -> async.startastask(a, cancellationtoken = cts.token)) async { let! t = async.awaittask ((task.whenany tasks).unwrap()) cts.cancel() return t }
Comments
Post a Comment