Task.WhenAny取消未完成的任务并超时

时间:2019-05-19 11:27:54

标签: c# .net async-await task-parallel-library

在我的应用中,我正在创建一些并发的Web请求,并且我对其中任何一个请求完成都感到满意,因此我使用的是方法Task.WhenAny

var urls = new string[] {
    "https://stackoverflow.com",
    "https://superuser.com",
    "https://www.reddit.com/r/chess",
};
var tasks = urls.Select(async url =>
{
    using (var webClient = new WebClient())
    {
        return (Url: url, Data: await webClient.DownloadStringTaskAsync(url));
    }
}).ToArray();
var firstTask = await Task.WhenAny(tasks);
Console.WriteLine($"First Completed Url: {firstTask.Result.Url}");
Console.WriteLine($"Data: {firstTask.Result.Data.Length:#,0} chars");
  

首次完成的网址:https://superuser.com
  数据:121.954个字符

我不喜欢这种实现方式,因为未完成的任务会继续下载不再需要的数据,并且浪费带宽,我希望为下一批请求保留这些带宽。因此,我正在考虑取消其他任务,但是我不确定该怎么做。我发现了如何使用CancellationToken来取消特定的Web请求:

public static async Task<(string Url, string Data)> DownloadUrl(
    string url, CancellationToken cancellationToken)
{
    try
    {
        using (var webClient = new WebClient())
        {
            cancellationToken.Register(webClient.CancelAsync);
            return (url, await webClient.DownloadStringTaskAsync(url));
        }
    }
    catch (WebException ex) when (ex.Status == WebExceptionStatus.RequestCanceled)
    {
        cancellationToken.ThrowIfCancellationRequested();
        throw;
    }
}

现在,我需要一个Task.WhenAny的实现,该实现将使用一组网址,并将使用我的DownloadUrl函数来获取响应速度最快的站点的数据,并将处理取消响应的逻辑。较慢的任务。如果它有一个 timeout 参数,可以为永无止境的任务提供保护,那就太好了。所以我需要这样的东西:

public static Task<Task<TResult>> WhenAnyEx<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, CancellationToken, Task<TResult>> taskFactory,
    int timeout)
{
    // What to do here?
}

有什么想法吗?

2 个答案:

答案 0 :(得分:5)

只需将相同的取消令牌传递给所有任务,就像这样:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
// here you specify how long you want to wait for task to finish before cancelling
int timeout = 5000;
cts.CancelAfter(timeout);
// pass ct to all your tasks and start them
await Task.WhenAny(/* your tasks here */);
// cancel all tasks
cts.Cancel();

此外,您需要阅读此线程以了解如何正确使用CancellationTokenWhen I use CancelAfter(), the Task is still running

答案 1 :(得分:0)

更新:基于Stephen Cleary's answerMSDNsvick's answer的更好的解决方案:

CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(TimeSpan.FromSeconds(1));

var tasks = urls.Select(url => Task.Run( async () => 
{
    using (var webClient = new WebClient())
    {
        token.Register(webClient.CancelAsync);
        var result = (Url: url, Data: await webClient.DownloadStringTaskAsync(url));
        token.ThrowIfCancellationRequested();
        return result.Url;
    }
}, token)).ToArray();

string url;
try
{
    // (A canceled task will raise an exception when awaited).
    var firstTask = await Task.WhenAny(tasks);
    url = (await firstTask).Url;
}   
catch (AggregateException ae) {
   foreach (Exception e in ae.InnerExceptions) {
      if (e is TaskCanceledException)
         Console.WriteLine("Timeout: {0}", 
                           ((TaskCanceledException) e).Message);
      else
         Console.WriteLine("Exception: " + e.GetType().Name);
   }
}

非最佳解决方案

可以通过添加在给定时间后等待并完成的任务来解决超时问题。然后,您检查哪个任务首先完成,如果是正在等待的任务,则有效地发生了超时。

Task timeout = Task.Delay(10000);
var firstTask = await Task.WhenAny(tasks.Concat(new Task[] {timeout}));
if(firstTask == timeout) { ... } //timed out
source.Cancel();