Following to this post, I want parallelize this method :
public IEnumerable<string> GetAllLogs(IEnumerable<IComputer> computers)
{
foreach (var cpt in computers)
{
foreach (var log in cpt.GetLogs())
{
yield return log;
}
}
}
I want the method "yield returns" a log when one of the method GetLogs is finished. If I have 4 computers which returns :
- Computer 01 : "a", "b", "c", "d", "e"
- Computer 02 : "1", "2", "3", "4", "5"
- Computer 03 : "alpha", "beta", "gamma", "delta", "epsilon"
- Computer 04 : "I", "II", "III", "IV", "V"
With the "sequential method", the output is :
a
b
c
d
e
1
2
3
4
5
alpha
beta
gamma
delta
epsilon
I
II
III
IV
V
And the methods runs in 20 seconds. there is a Thread.Sleep(1000)
in the GetLogs
method.
I want the output looks like this :
III
a
4
gamma
b
c
IV
5
d
II
beta
e
1
2
delta
alpha
3
epsilon
I
and runs in few seconds.
I want to the methods returns an IEnumerable
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…