There are a couple things you might do.
First, since this uses the ThreadPool
, you can use ThreadPool.SetMaxThreads
.
Second, you could introduce your own throttle along these lines:
let throttle = makeThrottle(8)
array
|> Seq.map (fun item -> async { do! throttle.Wait()
return f item})
|> Async.Parallel
|> Async.RunSynchronously
makeThrottle()
would not be too hard to write, but it would incur a little synchronization overhead. If you are trying to parallelize so many things that you're running out of memory, the throttle overhead is likely to be a non-issue. (Let me know if you need a sample for this kind of code.)
Finally, if this is really crashing things, it smells like you may be doing something wrong. The ThreadPool
typically (but not always) does a good job managing itself. But in various circumstances, designing your own throttle may be valuable to your app anyway.