本文介绍了以适当的方式在异步方法中合并三个任务的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是异步编程的新手.我想知道这些任务是否可以同时运行.考虑我有以下代码:

I am very new in async programming. I was wonder if there is any way for these tasks to run simultaneously. Consider I have this code:

public async Task<IList<WebData>> GetAllAsync()
{
    var twitterData = await ProvidetwitterDataAsync();
    var facebookData = await ProvidefacebookDataAsync();
    var linkedinData = await ProvidelinkedinDataAsync();
    return twitterData .Concat(facebookData ).Concat(linkedinData ).ToList();
}

我想是因为我使用await twitterData应该完成Task,并且在下一个Task开始之后,因为我想合并这三个列表的结果,所以我想知道有什么办法可以同时完成这三个任务?这种实施方式正确还是有更好的方法?

I think because I used await twitterData that Task should be done and after that next Task will be started, because I wanted to concat results of these three lists I wrote it in this way, I wanted to know is there any way that these three tasks can be done simultaneously? Is this way of implementation correct or is there a better way?

推荐答案

那么我们要小心并描述实际发生的事情.

Then let's be careful and describe what is actually happening carefully.

不要认为任务是同时运行 ,因为这意味着并发模型可能是正确的,也可能不是.您对任务的了解仅是它会异步完成.

Don't think of tasks as running simultaneously, since that implies a concurrency model that might or might not be true. All you know about a task is that it will complete asynchronously.

这样想:如果您有一些任务,例如支付我的账单"和修剪我的草坪",以及吃午餐",则可能要支付一半的账单,然后开始修剪草坪,然后在修剪过程中暂停一下在草坪上,吃一些午餐,多付一些账单,然后修剪草坪.您异步完成了这三个任务,但从未同步完成了这三个任务.您从来没有在吃午饭的同时修剪草坪.有时异步任务确实与其他任务同时在上运行 ,但并非总是如此,因此不要以为任务总是并发运行.

Think about it like this: if you have some tasks like "pay my bills" and "mow my lawn", and "eat lunch" you might pay half your bills, then start mowing the lawn, then pause halfway through mowing the lawn, eat some lunch, pay a few more bills, and then finish mowing the lawn. You did the three tasks asynchronously but never did them concurrently. You were never mowing the lawn at the same time as eating your lunch. Sometimes asynchronous tasks really are running at the same time as other tasks, but not always, so don't think of tasks as running concurrently always.

正确.关键见解是等待暂停异步工作流,直到任务完成. Await 不进行异步通话.调用返回的任务已经已经一个异步工作流程.等候仅表示如果此任务未完成,请找其他事情做,待完成后再回到这里".

Correct. The key insight is await pauses an asynchronous workflow until the task is completed. Await does NOT make a call asynchronous. The task returned by the call is already an asynchronous workflow. An await simply means "if this task is not complete then find something else to do and come back here when it is complete".

那么:您如何编写工作流,以便不对任务进行序列化?您在任务开始后等待.编写代码的正确方法是:

So: how do you write your workflow so that you are not serializing the tasks? You put the awaits after the tasks are started. The right way to write your code is:

public async Task<IList<WebData>> GetAllAsync()
{
  var twitter = ProvideTwitterDataAsync();
  var facebook = ProvideFacebookDataAsync();
  var linkedin = ProvideLinkedinDataAsync();
  var twitterData = await twitter;
  var facebookData = await facebook;
  var linkedinData = await linkedin;
  return twitterData .Concat(facebookData ).Concat(linkedinData ).ToList();
}

现在这三个任务是异步启动的,然后我们暂停工作流,直到所有三个任务都完成,然后将结果连接起来.

Now the three tasks are started asynchronously, and then we pause the workflow until all three are completed, and then we concatenate the results.

这篇关于以适当的方式在异步方法中合并三个任务的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 05:58