如何使用Task.WhenAny并实现重试 [英] How to use Task.WhenAny and implement retry
问题描述
我有一个解决方案,可以创建多个基于I/O的任务,并且我正在使用Task.WhenAny()来管理这些任务.但是通常许多任务会由于网络问题或请求节流等原因而失败.
I have a solution that creates multiple I/O based tasks and I'm using Task.WhenAny() to manage these tasks. But often many of the tasks will fail due to network issue or request throttling etc.
在使用Task.WhenAny()方法时,似乎找不到能够使我成功重试失败任务的解决方案.
I can't seem to find a solution that enables me to successfully retry failed tasks when using a Task.WhenAny() approach.
这是我在做什么:
var tasks = new List<Task<MyType>>();
foreach(var item in someCollection)
{
task.Add(GetSomethingAsync());
}
while (tasks.Count > 0)
{
var child = await Task.WhenAny(tasks);
tasks.Remove(child);
???
}
因此上述结构可用于完成任务,但是我还没有找到处理和重试失败任务的方法. await Task.WhenAny
引发AggregateException,而不是允许我检查任务状态.在异常处理程序中时,我不再有任何方法可以重试失败的任务.
So the above structure works for completing tasks, but I haven't found a way to handle and retry failing tasks. The await Task.WhenAny
throws an AggregateException rather than allowing me to inspect a task status. When In the exception handler I no longer have any way to retry the failed task.
推荐答案
我相信重试 个任务然后替换Task.WhenAny
-in-a-loop反模式会更容易与Task.WhenAll
I believe it would be easier to retry within the tasks, and then replace the Task.WhenAny
-in-a-loop antipattern with Task.WhenAll
例如,使用 Polly
:
E.g., using Polly
:
var tasks = new List<Task<MyType>>();
var policy = ...; // See Polly documentation
foreach(var item in someCollection)
tasks.Add(policy.ExecuteAsync(() => GetSomethingAsync()));
await Task.WhenAll(tasks);
或更简洁地说:
var policy = ...; // See Polly documentation
var tasks = someCollection.Select(item => policy.ExecuteAsync(() => GetSomethingAsync()));
await Task.WhenAll(tasks);
这篇关于如何使用Task.WhenAny并实现重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!