I know we can do async
stuff with let!
and use!
but is there a F# way of performing a set of operations in parallel (like Parallel.For(...)
& Parallel.ForEach(...)
in C#)?
You can use the Array.Parallel module for things like map
and iter
. Thereās also nuget packages for ParallelSeq and (highly recommended) the Nessos Streams library, which is a high-performance sequence library with support for parallelism.
@BanksySan I have made very good use of the PSeq module. Once you install the FSharp.Collections.ParallelSeq nuget package, sometimes itās just as easy as changing āSeq.iterā to āPSeq.iterā, and viola: 8 times as fast (for instance). YMMV.
Going with @ScottHutchinson here. PSeq
is dead simple to use and gets you pretty far rather quickly. If you want to invest a bit of time to learn a new concept Hopac is also a really nice way to write concurrent programs. Here is a row of very good blog posts describing the library.
For serious stuff I recommend Akka Streams (there is a nice F# wrapper, Akkling).
More answers there than I expected.
Why would one chose one over another?
TL;DR: because this topic can be quite complex. Choose the library/pattern that suits your present needs the best.
Because there are many strategies to solve concurrency problems. What you choose depends on many different factors:
- Do you mind to introduce additional dependencies to your project?
- Do you have a concurrency model you are already familiar with?
- Do you have special constraints which hinder you to choose a special concurrency model?
If you ājustā want to have a drop-in replacement for your Seq.map
operations (and operations that fall in the same category of general sequence processing) and you donāt mind the extra dependency PSeq
is more than enough.
If you donāt want to have extra dependencies @swlaschin wrote a blog post on āsimpleā asynchronous and parallel processing patterns in F# which you can find here.
If youād like to try out a concurrency pattern that is based on ConcurrentML Hopac is the way to go.
If you (or someone on your team) has a background (or interest) in agent/actor models you can either use the included MailboxProcessor or - as @vaskir recommended - you go to Akka.NET, which offers you a lot of extras.
Concurrent and asynchronous programming is a quite complex topic (also a reason why there are so many abstractions and libraries to make these tasks manageable). If you are looking for a good book in the .NET realm (with examples in C# and F#) this might interest you.
When using the async apiās donāt forget you can use Async.Parallel : seq<Async<'T>> -> Async<'T []>
Thatās pretty much equivalent to Parallel.ForEach
of system threading. https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/async.parallel['t]-method-[fsharp]