在过去,我很容易通过使用PLINQ将for循环变为多任务速度恶魔。
但是,由于处理了一系列馆藏,我的工作时间更加艰难。
我可能会自己解决这个问题,但我认为看看别人如何解决这个问题会很有用。
更新:我确信下面的代码存在概念性问题,但我无法完全用语言表达。如果有人能看到我在说什么,那很好,但是我会更多地考虑这个问题,并试着让整个问题更好(或者如果我意识到这完全是我的错误就删除它)
这是主要的逻辑:
public static void Execute()
{
var service = Locator.Get<ICakeService>();
using (var db = new Cake.Model.CakeContainer())
{
foreach (var campaigns in service.ExportCampaigns())
{
// --> THIS LOOP SHOULD BE EXECUTED IN A SEPARATE THREAD
foreach (var campaign in campaigns)
{
// ... logic to process a campaign ...
}
}
}
}
这是服务:
public IEnumerable<campaign[]> ExportCampaigns()
{
campaign[] result = null;
var service = new exportSoapClient("exportSoap");
// --> IF I DO A PARALLEL FOREACH HERE THE CLIENT CODE ABOVE
// STOPS MAKING SENSE TO ME...
foreach (var offer in this.ExportOffers())
{
var response = service.Campaigns(
api_key: this.apiKey,
offer_id: offer.offer_id);
yield return result;
}
}
答案 0 :(得分:2)
并行化的另一个选择是
Parallel.Foreach(service.ExportCampaigns(), campaigns =>
{
// --> THIS LOOP will be run in multiple threads at the same time
foreach (var campaign in campaigns)
{
// ... logic to process a campaign ...
}
});
如果内部foreach
工作量太少,这可能是比esskar's solution更好的选择。
这是没有lambadas的版本
public static void Execute(Action<campaign[]> action)
{
var service = Locator.Get<ICakeService>();
using (var db = new Cake.Model.CakeContainer())
{
Parallel.Foreach(service.ExportCampaigns(), action);
}
}
//This method will be executed in parallel for each element in the IEnumerable<campaign[]>.
private void ProcessCampaigns(campaign[] campaigns)
{
foreach (var campaign in campaigns)
{
// ... logic to process a campaign ...
}
}
// Call to Execute looks something like this:
command.Execute(ProcessCampaigns)
答案 1 :(得分:0)
使用System.Threading.Task中的Parallel Helper
Parallel.Foreach(campaigns, compaign =>
{
// ... logic to process a campaign ...
});
编辑:这是一个完整的例子
public static void Execute()
{
var service = Locator.Get<ICakeService>();
using (var db = new Cake.Model.CakeContainer())
{
foreach (var campaigns in service.ExportCampaigns())
{
ProcessCampaigns(campaigns);
}
}
}
private void ProcessCampaigns(IEnumerable<Campaign>> campaigns)
{
Parallel.Foreach(compaigns, ProcessCampaign);
}
private void ProcessCampaign(Campaign campaign)
{
// ... logic to process a campaign ...
}