我现在正处于需要决定如何构建异步代码的地步。 我需要从我的函数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();
听起来不错还是有更好的方法来完成工作?
答案 0 :(得分:3)
几个月前我们需要这样的东西来同时处理多个远程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());
}
答案 1 :(得分:2)
我想到了两个approcahes。
<强> A 即可。您现在的方式是使用ContinueWith
/ ContinueWhenAll
,如this answer和此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 });
或者你可以按照here所解释的那样连续延续。
另一种方法是使用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!"));
这里唯一要考虑的是你可以拥有可变数量的任务,但这很容易,我会让你解决这个问题。
<强>乙即可。另一种方法是使用.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(); });
}
以上内容与所显示的第一个代码相同,但框架会为您提供所有内容。
我希望这会有所帮助。
答案 2 :(得分:1)
我在这里只能想到两种主要方法:
请在收到后尽快汇总的地方。这可以通过 ContinueWith()方法完成。但是,您需要在聚合代码中处理同步,最后您仍需要等到所有任务完成。因此,只有聚合需要很长时间并且可以并行完成时,这种方法才有意义。
你这样做的方式 - 简单明了:)
至于使用 async ,我会投票使用TaskFactory.ContinueWhenAll()方法。这样你就不会阻塞任何线程,代码看起来比拥有多个等待(取决于品味)更好,并且可能会有更少的开销(可能取决于实现)。
答案 3 :(得分:0)
使用async
的正确方法是首先定义一个自然异步的CallServiceAsync
(即使用HttpClient
或TaskFactory.FromAsync
Begin
包装End
} / Task.Run
方法)。它应该不使用CallServiceAsync
。
一旦你有一个自然异步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);
}
,那么你可以发出20个同时调用和(异步)等待它们:
ConfigureAwait(false)
这种方法根本不会阻止任何线程。
您可以使用XDocument[] results = await task.WhenAll(task1, task2, task3, ...).ConfigureAwait(false);
来提高性能:
{{1}}