Is there any way that Async.Parallel can be limited/ throttled by introducing a scheduler? I'm looking to execute a Seq of Async<'a> in parallel but don't want to exceed a certain hourly-limit.
I could use a shared mutable variable that each Async<'a> examines but I'd like to avoid this if possible.
Under the cover, the
Async.Paralleloperation uses the standard .NET thread pool. So, you could configure the thread pool, but that's probably not a good idea (you should not be blocking threads in a thread pool).If I wanted to implement some throttling, I would probably create an F# agent for this. Agents give you a pretty simple way to coordinate the concurrency - it is probably more code than using mutable variable (for this purpose), but it gives you a nice abstraction:
To use this, you can create an agent with a specified limit and then call
Enqueueto add your work items:This is solving a bit different problem than the one you have - but it should show the direction (rather than having the
Completednotification, you probably want to have someasyncin the background that sends a specified number of "tokens" every hour).