[英]Parallel tasks vs delegates
我现在正处于需要决定如何构建异步代码的地步。 我需要从我的函数Func1()一次调用20个不同的Web服务,当它们都返回xml答案时,将所有结果连接到一个大的xml。
我想过使用TPL任务。 这样的事情:
var task = Task.Factory.StartNew (call the web service1...);
var task2 = Task.Factory.StartNew (call the web service2...);
var task3 = Task.Factory.StartNew (call the web service3...);
task.WaitAll();
这听起来不错还是有更好的方法来完成工作?
几个月前我们需要这样的东西来同时处理多个远程URL。 我们通过从SemaphoreSlim类派生我们自己的类来实现它。
你可以实现这样的事情:
/// <summary>
/// Can be used to process multiple URL's concurrently.
/// </summary>
public class ConcurrentUrlProcessor : SemaphoreSlim
{
private int initialCount;
private int maxCount;
private readonly HttpClient httpClient;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Threading.SemaphoreSlim" /> class, specifying the initial number of requests that can be granted concurrently.
/// </summary>
/// <param name="initialCount">The initial number of requests for the semaphore that can be granted concurrently.</param>
public ConcurrentUrlProcessor(int initialCount)
:base(initialCount)
{
this.initialCount = initialCount;
this.maxCount = int.MaxValue;
this.httpClient = new HttpClient();
}
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Threading.SemaphoreSlim" /> class, specifying the initial and maximum number of requests that can be granted concurrently.
/// </summary>
/// <param name="initialCount">The initial number of requests for the semaphore that can be granted concurrently.</param>
/// <param name="maxCount">The maximum number of requests for the semaphore that can be granted concurrently.</param>
public ConcurrentUrlProcessor(int initialCount, int maxCount)
: base(initialCount, maxCount)
{
this.initialCount = initialCount;
this.maxCount = maxCount;
this.httpClient = new HttpClient();
}
/// <summary>
/// Starts the processing.
/// </summary>
/// <param name="urls">The urls.</param>
/// <returns>Task{IEnumerable{XDocument}}.</returns>
public virtual async Task<IEnumerable<XDocument>> StartProcessing(params string[] urls)
{
List<Task> tasks = new List<Task>();
List<XDocument> documents = new List<XDocument>();
SemaphoreSlim throttler = new SemaphoreSlim(initialCount, maxCount);
foreach (string url in urls)
{
await throttler.WaitAsync();
tasks.Add(Task.Run(async () =>
{
try
{
string xml = await this.httpClient.GetStringAsync(url);
//move on to the next page if no xml is returned.
if (string.IsNullOrWhiteSpace(xml))
return;
var document = XDocument.Parse(xml);
documents.Add(document);
}
catch (Exception)
{
//TODO: log the error or do something with it.
}
finally
{
throttler.Release();
}
}));
}
await Task.WhenAll(tasks);
return documents;
}
}
并按单位测试:
[Test]
public async void CanProcessMultipleUrlsTest()
{
string[] urls = new string[]
{
"http://google.nl",
"http://facebook.com",
"http://linkedin.com",
"http://twitter.com"
};
IEnumerable<XDocument> documents = null;
ConcurrentUrlProcessor processor = new ConcurrentUrlProcessor(100);
documents = await processor.StartProcessing(urls);
Assert.AreEqual(4, documents.Count());
}
我想到了两个approcahes。
A。 你现在的样子,现在这样做,但使用continuation ContinueWith
/ ContinueWhenAll
在描述这个答案并在此articale 。 因此,对于您的情况,您可以使用子任务的单个延续,所以
TaskCreationoptions op = TaskCreationOptions.AttachedToParent;
Task.Factory.StartNew(() =>
{
var task1 = Task.Factory.StartNew (CallService(1));
var task2 = Task.Factory.StartNew (CallService(2));
var task3 = Task.Factory.StartNew (CallService(3));
})
.ContinueWith(ant => { SomeOtherselegate });
或者您可以按照此处的说明链接延续。
另一种方法是使用ContinueWhenAll
。
var task1 = Task.Factory.StartNew (CallService(1));
var task2 = Task.Factory.StartNew (CallService(2));
var task3 = Task.Factory.StartNew (CallService(3));
var continuation = Task.Factory.ContinueWhenAll(
new[] { task1, task2, task3 }, tasks => Console.WriteLine("Done!"));
这里唯一要考虑的是你可以拥有可变数量的任务的方式,但这很容易,我会让你解决这个问题。
B。 另一种方法是使用.NET4.5 +和async
/ await
。 所以你的代码会是这样的
private async void CallAllServicesAsync()
{
await CallServiceAsync(1);
await CallServiceAsync(2);
await CallServiceAsync(3);
}
哪里
private Task CallServiceAsync(int serviceNumber)
{
return Task.Run(() = > { SomeMethod(); });
}
以上内容与所示的第一个代码相同,但框架会为您提供所有内容。
我希望这有帮助。
我在这里只能想到两种主要方法:
有一些地方,你收到后会尽快汇总。 这将通过ContinueWith()方法完成。 但是,您需要在聚合代码中处理同步,最后您仍需要等到所有任务完成。 因此,只有聚合需要很长时间并且可以并行完成时,这种方法才有意义。
你做的方式 - 好又简单:)
至于使用异步 ,我会投票使用TaskFactory.ContinueWhenAll()方法。 这样你就不会阻塞任何线程,代码看起来会比多次等待更好(取决于品味)并且可能会有更少的开销(可能取决于实现)。
使用async
的正确方法是首先定义一个自然异步的CallServiceAsync
(即,使用HttpClient
或者是围绕Begin
/ End
方法的TaskFactory.FromAsync
包装器)。 它不应该使用Task.Run
。
一旦你有一个自然异步的CallServiceAsync
,那么你可以发出20个同时调用和(异步)等待它们:
public async Task<XDocument> GetAllDataAsync()
{
var task1 = CallServiceAsync(1);
var task2 = CallServiceAsync(2);
var task3 = CallServiceAsync(3);
...
XDocument[] results = await task.WhenAll(task1, task2, task3, ...);
return JoinXmlDocuments(results);
}
这种方法根本不会阻塞任何线程。
您可以通过使用ConfigureAwait(false)
使其性能稍微提高:
XDocument[] results = await task.WhenAll(task1, task2, task3, ...).ConfigureAwait(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.