1

过去,我很容易通过使用 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;
        }
    }
4

2 回答 2

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 的解决方案更好的选择。

这是一个没有 lambdas 的版本

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)
于 2012-05-18T17:04:00.133 回答
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 ...
}
于 2012-05-18T16:53:02.397 回答