Advertisement
ptrelford

Run N units of work in parallel with retry

Apr 17th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.40 KB | None | 0 0
  1. let rnd = System.Random()
  2.  
  3. let work x () =
  4.     if rnd.Next(2) = 1 then failwith "Bang"
  5.     printfn "Working %d" x
  6.  
  7. let rec retry f = async {
  8.     try f()
  9.     with e ->
  10.         printfn "Retrying"
  11.         do! Async.Sleep 500
  12.         return! retry f
  13.     }
  14.  
  15. let task =
  16.     [for i in 1..10 -> work i |> retry]
  17.     |> Async.Parallel
  18.  
  19. do  Async.RunSynchronously(task, timeout=1000)
  20.     |> ignore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement