Semaphore thread throttling with async/await

前端 未结 1 2067
小鲜肉
小鲜肉 2021-02-13 17:35

I recently came across an example of throttling threads for async/await calls. After analyzing and playing with the code on my machine, I came up with a slightly different way o

相关标签:
1条回答
  • It's not much leaner, just a little bit. Usually, I avoid ContinueWith in async code because await is cleaner and has more async-friendly default semantics. Optimize for developer time first, then optimize for other considerations.

    Your code does change the semantics slightly: in the original code, LongRunningTask was executed from a thread pool context, and in your code it's executed from whatever the CallThrottledTasks context is. Also, your code won't propagate exceptions from LongRunningTask cleanly; Task<T>.Result will wrap exceptions in AggregateException, while await will not do any wrapping.

    0 讨论(0)
提交回复
热议问题