对多个任务使用 async/await [英] Using async/await for multiple tasks
问题描述
我使用的是完全异步的 API 客户端,也就是说,每个操作要么返回 Task
要么返回 Task
,例如:
static async Task DoSomething(int siteId, int postId, IBlogClient client){等待 client.DeletePost(siteId, postId);//调用API客户端Console.WriteLine("已删除帖子 {0}.", siteId);}
使用 C# 5 async/await 运算符,启动多个任务并等待它们全部完成的正确/最有效方法是什么:
int[] ids = new[] { 1, 2, 3, 4, 5 };Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait());
或:
int[] ids = new[] { 1, 2, 3, 4, 5 };Task.WaitAll(ids.Select(i => DoSomething(1, i, blogClient)).ToArray());
由于 API 客户端在内部使用 HttpClient,我希望这会立即发出 5 个 HTTP 请求,并在每个请求完成时写入控制台.
int[] ids = new[] { 1, 2, 3, 4, 5 };Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait());
尽管您与上述代码并行运行操作,但此代码会阻塞每个操作运行的每个线程.例如,如果网络调用需要 2 秒,则每个线程挂起 2 秒,不做任何事情,除了等待.
int[] ids = new[] { 1, 2, 3, 4, 5 };Task.WaitAll(ids.Select(i => DoSomething(1, i, blogClient)).ToArray());
另一方面,上面带有 WaitAll
的代码也会阻塞线程,并且您的线程在操作结束之前将无法自由处理任何其他工作.
推荐方法
我更喜欢 WhenAll
它将并行异步执行您的操作.
public async Task DoWork() {int[] ids = new[] { 1, 2, 3, 4, 5 };await Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient)));}
<块引用>
事实上,在上面的例子中,你甚至不需要await
,你可以直接从方法中返回,因为你没有任何continuations:
公共任务DoWork(){int[] ids = new[] { 1, 2, 3, 4, 5 };return Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient)));}
为了支持这一点,这里有一篇详细的博客文章替代方案及其优点/缺点:如何以及在何处使用 ASP.NET Web API 并发异步 I/O
I'm using an API client that is completely asynchrounous, that is, each operation either returns Task
or Task<T>
, e.g:
static async Task DoSomething(int siteId, int postId, IBlogClient client)
{
await client.DeletePost(siteId, postId); // call API client
Console.WriteLine("Deleted post {0}.", siteId);
}
Using the C# 5 async/await operators, what is the correct/most efficient way to start multiple tasks and wait for them all to complete:
int[] ids = new[] { 1, 2, 3, 4, 5 };
Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait());
or:
int[] ids = new[] { 1, 2, 3, 4, 5 };
Task.WaitAll(ids.Select(i => DoSomething(1, i, blogClient)).ToArray());
Since the API client is using HttpClient internally, I would expect this to issue 5 HTTP requests immediately, writing to the console as each one completes.
int[] ids = new[] { 1, 2, 3, 4, 5 };
Parallel.ForEach(ids, i => DoSomething(1, i, blogClient).Wait());
Although you run the operations in parallel with the above code, this code blocks each thread that each operation runs on. For example, if the network call takes 2 seconds, each thread hangs for 2 seconds w/o doing anything but waiting.
int[] ids = new[] { 1, 2, 3, 4, 5 };
Task.WaitAll(ids.Select(i => DoSomething(1, i, blogClient)).ToArray());
On the other hand, the above code with WaitAll
also blocks the threads and your threads won't be free to process any other work till the operation ends.
Recommended Approach
I would prefer WhenAll
which will perform your operations asynchronously in Parallel.
public async Task DoWork() {
int[] ids = new[] { 1, 2, 3, 4, 5 };
await Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient)));
}
In fact, in the above case, you don't even need to
await
, you can just directly return from the method as you don't have any continuations:public Task DoWork() { int[] ids = new[] { 1, 2, 3, 4, 5 }; return Task.WhenAll(ids.Select(i => DoSomething(1, i, blogClient))); }
To back this up, here is a detailed blog post going through all the alternatives and their advantages/disadvantages: How and Where Concurrent Asynchronous I/O with ASP.NET Web API
这篇关于对多个任务使用 async/await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!