Multithreading – how to limit to asynchronous SEQ in f# Number of threads created by map operation?

The current setting is like this

array
|> Seq.map (fun item -> async { return f item})
|> Async.Parallel
|> Async.RunSynchronously

The problem is that this tends to create too many threads and crash the application regularly

In this case, how to limit the number of threads (for example, environment. Processorcount)?

Solution

If you want to parallelize CPU intensive computing with arrays (or any sequences) as input, it is best to use the PSEQ module in f# PowerPack (although available only on. Net 4.0) It provides many standard array A parallel version of the XYZ function For more information, you can also view f# translation in 0700 samples

Problem solving code is simpler than using workflow:

array |> PSeq.map f
      |> PSeq.toArray

Some of the differences between the two options are:

>PSEQ is used Net 4.0, which is optimized for processing a large number of CPU intensive tasks. > Asynchrony is implemented in the f# library and supports asynchronous (non blocking) operations, such as I / O in concurrent operations

In short, if you need asynchronous operations (such as I / O), async is the best choice If you have a large number of CPU intensive tasks, PSEQ may be a better choice (on. Net 4.0)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>