本文介绍了TPL数据流,什么帖子()和SendAsync()之间的功能区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到困惑邮通()或SendAsync()发送项目之间的差异。我的理解是,在所有情况下,一旦项目达成一个数据块的输入缓冲器,控制返回到调用上下文,正确吗?那么,为什么我会永远需要SendAsync?如果我的假设是不正确,我在想,相反,为什么会有人曾经使用后()如果使用的数据块的整体思路是建立一个并行异步和环境。

I am confused about the difference between sending items through Post() or SendAsync(). My understanding is that in all cases once an item reached the input buffer of a data block, control is returned to the calling context, correct? Then why would I ever need SendAsync? If my assumption is incorrect then I wonder, on the contrary, why anyone would ever use Post() if the whole idea of using data blocks is to establish a concurrent and async environment.

据我了解,而SendAsync返回布尔的awaitable任务当然技术上在该职位()的区别返回一个布尔值。但是,这是否有什么影响?当将一个布尔(我的理解是确认该项目是否被放置在数据块的队列与否)的回报不断被推迟?据我所知,异步/的await并发框架的总体思路,但在这里不作一大堆意义,因为除了一个布尔值,无论是为了传入的项目的结果永远不会返回给调用者,而是放置在一个出队列,要么转发到链接的数据块或丢弃。

I understand of course the difference technically in that Post() returns a bool whereas SendAsync returns an awaitable Task of bool. But what implications does that have? When would the return of a bool (which I understand is a confirmation whether the item was placed in the queue of the data block or not) ever be delayed? I understand the general idea of the async/await concurrency framework but here it does not make a whole lot sense because other than a bool the results of whatever is done to the passed-in item is never returned to the caller but instead placed in an "out-queue" and either forwarded to linked data blocks or discarded.

和是否有发送项目时,这两种方法之间的性能差异?

And is there any performance difference between the two methods when sending items?

推荐答案

要看到差距,你需要的情况下块将推迟他们的消息。在这种情况下,发表将返回立即,而 SendAsync 会返回一个工作当块决定如何处理消息做将完成。在工作将有一个真正导致如果接受邮件,以及结果如果不。

To see the difference, you need a situation where blocks will postpone their messages. In this case, Post will return false immediately, whereas SendAsync will return a Task that will be completed when the block decides what to do with the message. The Task will have a true result if the message is accepted, and a false result if not.

的推迟情况的一个例子是一个非贪婪加入。一个简单的例子是,当你设置 BoundedCapacity

One example of a postponing situation is a non-greedy join. A simpler example is when you set BoundedCapacity:

[TestMethod]
public void Post_WhenNotFull_ReturnsTrue()
{
    var block = new BufferBlock<int>(new DataflowBlockOptions {BoundedCapacity = 1});

    var result = block.Post(13);

    Assert.IsTrue(result);
}

[TestMethod]
public void Post_WhenFull_ReturnsFalse()
{
    var block = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 });
    block.Post(13);

    var result = block.Post(13);

    Assert.IsFalse(result);
}

[TestMethod]
public void SendAsync_WhenNotFull_ReturnsCompleteTask()
{
    // This is an implementation detail; technically, SendAsync could return a task that would complete "quickly" instead of already being completed.
    var block = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 });

    var result = block.SendAsync(13);

    Assert.IsTrue(result.IsCompleted);
}

[TestMethod]
public void SendAsync_WhenFull_ReturnsIncompleteTask()
{
    var block = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 });
    block.Post(13);

    var result = block.SendAsync(13);

    Assert.IsFalse(result.IsCompleted);
}

[TestMethod]
public async Task SendAsync_BecomesNotFull_CompletesTaskWithTrueResult()
{
    var block = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 });
    block.Post(13);
    var task = block.SendAsync(13);

    block.Receive();

    var result = await task;
    Assert.IsTrue(result);
}

[TestMethod]
public async Task SendAsync_BecomesDecliningPermanently_CompletesTaskWithFalseResult()
{
    var block = new BufferBlock<int>(new DataflowBlockOptions { BoundedCapacity = 1 });
    block.Post(13);
    var task = block.SendAsync(13);

    block.Complete();

    var result = await task;
    Assert.IsFalse(result);
}

这篇关于TPL数据流,什么帖子()和SendAsync()之间的功能区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:13