是否有可能使用并行任务并返回 IEnumerable<T> 的方法? [英] Is it possible to have a method using Parallel tasks and returns IEnumerable&lt;T&gt;

查看:22
本文介绍了是否有可能使用并行任务并返回 IEnumerable<T> 的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望该方法并行执行任务,并在任务完成后返回"结果.是否有可能有这样的东西:

I want that the method executes tasks in parallel and when the task is finished "yield return" the result. Is it possible to have something like that :

   public IEnumerable<string> GetAllLogs()
   {
     var computers = GetComputers()
                            .Where(cpt => cpt.IsOnline);

     Parallel.ForEach(computers, c => c.GetLogs());

     // How to 'yield return' ?
   }

谢谢!!!

也许我之前的样本在这里不够明确,一个新的(我希望)更明确的 ;-)

Maybe my previous sample was not enough explicit here a new and (I hope) more explicit one ;-)

我想知道如何并行化 GetAllLogs 方法:

I want to know how to parallize the GetAllLogs method :

public class ComputerManager
{
    public IEnumerable<string> GetAllLogs(IEnumerable<IComputer> computers)
    {
        foreach (var cpt in computers)
        {
            // How to Parallelize the foreach bloc and 
            // use a 'yield return' to keep the IEnumerable<string> return type ?
            foreach (var log in cpt.GetLogs())
            {
                yield return log;
            }
        }
    }
}

    static void Main(string[] args)
    {
        ComputerManager cm = new ComputerManager();
        IComputer[] computers = new IComputer[] {new Computer(), new Computer2(), new Computer3(), new Computer4() };

        Stopwatch sw = new Stopwatch();
        sw.Start();

        foreach (string s in cm.GetAllLogs(computers))
        {
            Console.WriteLine(s);
        }

        sw.Stop();
        Console.WriteLine("Terminé en : {0}" , sw.ElapsedMilliseconds);
        Console.Read();
    }
}

public interface IComputer
{
    IEnumerable<string> GetLogs();
}


public class Computer : IComputer
{
    public IEnumerable<string> GetLogs()
    {
        string[] alphabet = new string[] { "a", "b", "c", "d", "e"};

        foreach (var letter in alphabet)
        {
            Thread.Sleep(1000);
            yield return letter;
        }
    }
}

public class Computer2 : IComputer
{
    public IEnumerable<string> GetLogs()
    {
        string[] figures = new string[] { "1", "2", "3", "4", "5" };

        foreach (var figure in figures)
        {
            Thread.Sleep(1000);
            yield return figure;
        }
    }
}


public class Computer3 : IComputer
{
    public IEnumerable<string> GetLogs()
    {
        string[] greekAlphabet = new string[] { "alpha", "beta", "gamma", "delta", "epsilon" };

        foreach (var letter in greekAlphabet)
        {
            Thread.Sleep(1000);
            yield return letter;
        }
    }
}


public class Computer4 : IComputer
{
    public IEnumerable<string> GetLogs()
    {
        string[] romanFigures = new string[] { "I", "II", "III", "IV", "V" };

        foreach (var s in romanFigures)
        {
            Thread.Sleep(1000);
            yield return s;
        }
    }
}

推荐答案

如果你想在并行执行一开始就返回:

If you want to return as soon as the parallel execution begins :

public class ComputerManager
{
    public IEnumerable<string> GetAllLogs(IEnumerable<IComputer> computers)
    {
        return computers.AsParallel().SelectMany(c => c.GetLogs());
    }
}

如果你想在并行执行完成后返回:

If you want to return when the parallel execution finished :

public class ComputerManager
{
    public IEnumerable<string> GetAllLogs(IEnumerable<IComputer> computers)
    {
        return computers.AsParallel().SelectMany(c => c.GetLogs()).ToArray();
    }
}

这篇关于是否有可能使用并行任务并返回 IEnumerable<T> 的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆