[英]What is the difference between specifying Action directly or Task-Generator with TPL dataflow ActionBlock?
What is the difference between specifying Action
directly or Func<Task>
with .NET TPL Dataflow ActionBlock
? 直接指定Action
或使用.NET TPL Dataflow ActionBlock
指定Func<Task>
有什么区别?
Direct Action: 直接行动:
new ActionBlock<Message[]>(x => DoSomething(x))
Task: 任务:
new ActionBlock<Message[]>(x => Task.Run(() => DoSomething(x)))
I'm trying to understand the differences in regard to parallel execution ( MaxDegreeOfParallelism
> 1). 我试图了解并行执行方面的差异( MaxDegreeOfParallelism
> 1)。
TPL Dataflow supports both async
and synchronous delegates so there's no difference regarding the degree of parallelism. TPL Dataflow支持async
和同步委托,因此并行度没有差异。 It "knows" to await
the returned task that represents the item's asynchronous execution and not continue on to the next item. 它“知道” await
表示该项目的异步执行的返回任务,而不继续进行下一个项目。 In both cases no more than MaxDegreeOfParallelism
items would be processed concurrently. 在这两种情况下, MaxDegreeOfParallelism
只能同时处理MaxDegreeOfParallelism
项目。 The first option would use the synchronous delegate (ie Action
) while the second the async
one (ie Func<Task>
). 第一种选择将使用的同步委托(即, Action
),而第二所述async
一个(即Func<Task>
)。
However using Task.Run
would take up an extra thread for each item's execution only to release it back to the thread pool after it's done. 但是,使用Task.Run
将为每个项目的执行占用一个额外的线程,只是在完成后才将其释放回线程池。 It's offloading work to another thread with no reason. 它没有任何理由将工作转移到另一个线程。 So don't use it, it adds no value. 因此,请勿使用它,它不会增加任何价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.