Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active April 3, 2018 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/60c9c88bec822463bd4af86371f2663b to your computer and use it in GitHub Desktop.
Save Szer/60c9c88bec822463bd4af86371f2663b to your computer and use it in GitHub Desktop.
open System
let random = Random(int DateTime.UtcNow.Ticks)
let asyncMockup s = async {
let sleepFor = int (random.NextDouble() * 3000.)
//let sleepFor = 2000
do! Async.Sleep sleepFor
printfn "resulted %s" s
return sprintf "resulted %s" s
}
let inline (>>-) x f = async.Bind(x, f >> async.Return)
let requestMasterAsync limit urls =
let results = Array.zeroCreate (List.length urls)
let chunks =
urls
|> Seq.chunkBySize limit
|> Seq.indexed
async.For (chunks, fun (i, chunk) ->
chunk
|> Seq.map asyncMockup
|> Async.Parallel
>>- Seq.iteri (fun j r -> results.[i*limit+j]<-r))
>>- fun _ -> results
let results =
List.init 20 string
|> requestMasterAsync 3
|> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment