简体   繁体   English

我可以嵌套Parallel.Invoke方法吗?

[英]Can I nest Parallel.Invoke methods?

I was wondering whether it's possible or recommended to have multiple layers of the Parallel.Invoke method or whether it is likely to cause problems / slow the application. 我想知道是否有可能或建议使用Parallel.Invoke方法的多层,或者是否有可能引起问题/降低应用程序速度。

Is there a best practice for this? 是否有最佳做法?

Parallel.Invoke(
    () => Invoke("orange"),
    () => Invoke("apples"),
    () => Invoke("pears")
);           

public static void Invoke(string name)
{
    Parallel.Invoke(
        () => test.testapp(name),
        () => test.testapp(name),
        () => test.testapp(name)
    );
}

It's possible, but there might be no benefit - it really depends on what those jobs are doing. 有可能,但可能没有任何好处-这实际上取决于这些工作在做什么。

It might be beneficial if they're essentially asynchronous tasks (eg on the database or external API) which can be executed in parallel. 如果它们本质上是可以并行执行的异步任务(例如在数据库或外部API上),则可能会有所帮助。 However, if they're CPU-intensive tasks then you're probably just going to give extra work to the scheduler for no real benefit. 但是,如果它们是CPU密集型任务,那么您可能只是给调度程序增加了额外的工作,却没有真正的好处。

There's an article here which contains some benchmarks using the Parallel libary, it might give you a good idea when to use this. 这里有一篇文章其中包含一些使用Parallel库的基准测试,可能会给您一个何时使用它的好主意。

Results of this test: 测试结果:

Simulating calling external resources: 模拟调用外部资源:

Not Nested: 00:00:14.6155628
Nested: 00:00:09.0907248

CPU intensive tasks: CPU密集型任务:

Not Nested: 00:00:05.6169188
Nested: 00:00:06.1500767

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM