nicolas
nicolas

Reputation: 9815

cost of Async.Start

In a mailboxprocessor loop, I read from a blocking collection items previously stored in such collection. Since I use the same loop for writing to such collection, I need launch it as a thread.

async { process(queue.Take()) } |> Aysync.Start

The execution is of my whole code is slow (relatively speaking) and I suspect the cause is the new thread I launch, although I kickstarted the thread pool with

let toto = ThreadPool.SetMinThreads(300,300)

Another hint that the contention point might be here is that if I only launch when the queue is empty (and lock the whole section)), I have highly varying runtime, from 350 ms to 7s, while if if dont it stays around 5-10 s.

My questions are :

Upvotes: 1

Views: 230

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243096

If you need to create hundreds of threads to run an I/O bound computation then there is probably something wrong. If a computation is I/O bound then it should be possible to run it using relatively small number of threads - if it is fully asynchronous it means that the threads will not be blocked during any waiting.

So, I think the first thing to look for in your program are places where a thread is blocked and replace that with waiting that is asynchronous.

One suspicious thing in your code sample is the queue, which is probably blocking when you call Take, at least, that's how BlockingCollection in .NET behaves. You can try replacing that with BlockingQueueAgent, which implements the same functionality using F# agents but provides asynchronous AsyncTake method that can be called without blocking a thread.

Upvotes: 1

Related Questions