根据我的理解,personIds.Batch(100)
只需将批次personIds
分成大小(100)个桶。
//method1
foreach (var personIdsBatch in personIds.Batch(100))
{
customResults.AddRange(GetCustomResultsByBatch(address, username, password, personIdsBatch));
}
//method2
customResults.AddRange(GetCustomResultsByBatch(address, username, password, personIds));
上述两种方法都会按顺序为每个人调用您的自定义 API,同时method1
添加了额外的逻辑来处理相同的任务。
Azure Batch 是否支持异步/等待?
根据您的代码,我将IDotNetActivity
实现定义如下,您可以参考:
public class MyDotNetActivity : IDotNetActivity
{
public IDictionary<string, string> Execute(IEnumerable<LinkedService> linkedServices, IEnumerable<Dataset> datasets, Activity activity, IActivityLogger logger)
{
return ExecuteAsync(linkedServices, datasets, activity, logger).Result;
}
async Task<IDictionary<string, string>> ExecuteAsync(IEnumerable<LinkedService> linkedServices, IEnumerable<Dataset> datasets, Activity activity, IActivityLogger logger)
{
List<int> personIds = await GetPersonIds("{clientAddress}", "{clientUsername}", "{clientPassword}");
var tasks = new List<Task<List<CustomApiResult>>>();
foreach (var personIdsBatch in personIds.Batch(100))
{
tasks.AddRange(GetCustomResultsByBatch("{address}", "{username}", "{password}", "{personIdsBatch}"));
}
var taskResults = await Task.WhenAll(tasks);
List<CustomApiResult> customResults = taskResults.SelectMany(r=>r).ToList();
//process the custom api results
return new Dictionary<string, string>();
}
async Task<List<CustomApiResult>> GetCustomResultsByBatch(string address, string username, string password, IEnumerable<int> personIdsBatch)
{
//Get Custom Results By Batch
return new List<CustomApiResult>();
}
async Task<List<int>> GetPersonIds(string clientAddress, string clientUsername, string clientPassword)
{
//load a list of people from a file in Azure Blob storage
return new List<int>();
}
}
另外,我假设您可以按如下方式利用Parallel.ForEach来并行执行您的同步作业:
List<int> personIds = GetPersonIds(clientAddress, clientUsername, clientPassword);
var customResults = new List<CustomApiResult>();
Parallel.ForEach(personIds.Batch(100),
new ParallelOptions()
{
MaxDegreeOfParallelism=5
},
(personIdsBatch) =>
{
var results = GetCustomResultsByBatch(address, username, password, personIdsBatch);
lock (customResults)
{
customResults.AddRange(results);
}
});