Created
March 18, 2018 04:31
-
-
Save locatw/914a4dbb0d563e93638e2465eba0e3f8 to your computer and use it in GitHub Desktop.
Parallel execution patterns of F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let func1 () = | |
| async { | |
| printfn "func1 start" | |
| Async.Sleep 3000 |> Async.RunSynchronously | |
| printfn "func1 end" | |
| } | |
| let func2 () = | |
| async { | |
| printfn "func2 start" | |
| Async.Sleep 1000 |> Async.RunSynchronously | |
| printfn "func2 end" | |
| } | |
| let runSequentially () = | |
| async { | |
| printfn "runSequentially start" | |
| do! func1 () | |
| do! func2 () | |
| printfn "runSequentially end" | |
| } | |
| let runParallel1 () = | |
| async { | |
| printfn "runParallel1 start" | |
| let! childFunc1 = func1 () |> Async.StartChild | |
| do! func2 () | |
| do! childFunc1 | |
| printfn "runParallel1 end" | |
| } | |
| let runParallel2 () = | |
| async { | |
| printfn "runParallel2 start" | |
| [ func1 (); func2 () ] | |
| |> Async.Parallel | |
| |> Async.RunSynchronously | |
| |> ignore | |
| printfn "runParallel2 end" | |
| } | |
| runSequentially () |> Async.RunSynchronously | |
| printfn "" | |
| runParallel1 () |> Async.RunSynchronously | |
| printfn "" | |
| runParallel2 () |> Async.RunSynchronously |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment