您可以强制Parallel.Invoke使用多线程? [英] Can you force Parallel.Invoke to use multiple threads?

查看:148
本文介绍了您可以强制Parallel.Invoke使用多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这样的任务并行库在.aspx页面中:

I am using the task parallel library like this in a .aspx page:

 Parallel.Invoke(
                new Action[]
                    {
                        () => { users= service.DoAbc(); },
                        () => { products= service.DoDef(); }
                    });



以前我是发射了每次调用一个线程,它是更加敏感,这是现在,当我使用Parallel.Invoke。

Previously I was firing off a thread per call, and it was more responsive that it is now when I use Parallel.Invoke.

我应该承担的TPL库将做什么是最好的或者是有让我调整它因此它实际上并行确实调用的方法吗?

Should I assume the TPL library will do what's best or is there a way for me to tweak it so it actually does calls in parallel?

我猜它归结为硬件的类型我的网站上运行,我相信这是一个虚拟机。

I guess it comes down to the type of hardware my website is running on, which I believe is a VM.

每个我的电话使得HTTP请求来从一个API调用的结果。

Each of my calls makes a http request to fetch results from a API call.

推荐答案

Parallel.Invoke将运行的方法平行除非这比依次运行这些更昂贵,或有在threadpool.This没有可用的线程是一个优化,不是一个问题。在正常情况下,你不应该试图去猜测的框架,只是让它做它的工作。

Parallel.Invoke will run you methods in parallel unless this is more expensive than running them sequentially, or there are no available threads in the threadpool.This is an optimization, not an issue. Under normal circumstances you shouldn't try to second-guess the framework and just let it do its job.

您应该考虑重写这种行为,如果你想调用一些长-running IO绑定的方法。 Parallel.Invoke使用默认的TaskScheduler因为有核心(不知道有多少),以避免超载的CPU,它使用一样多的线程。这不,如果你的行动只是等待一些IO或网络调用来完成的问题。

You should consider overriding this behavior if you want to invoke some long-running IO-bound methods. Parallel.Invoke uses the default TaskScheduler which uses about as many threads as there are cores (not sure how many) to avoid overloading the CPU. This is not an issue if your actions just wait for some IO or network call to complete.

您可以指定使用Parallel.Invoke的最大线程数(ParallelOptions,操作[])] 1 覆盖。您也可以使用 ParallelOptions 类来传递。取消令牌或specifiy定制的TaskScheduler,例如一个允许您使用更多的线程比默认的调度

You can specify the maximum number of threads using the Parallel.Invoke(ParallelOptions,Action[])]1 override. You can also use the ParallelOptions class to pass a cancellation token or specifiy a custom TaskScheduler, eg one that allows you to use more threads than the default scheduler.

您可以重写你这样的代码:

You can rewrite your code like this:

Parallel.Invoke(
            new ParallelOptions{MaxDegreeOfParallelism=30},
            new Action[]
                {
                    () => { users= service.DoAbc(); },
                    () => { products= service.DoDef(); }
                });



不过,你不应该试图修改默认选项,除非你找到一个实际的性能问题。你可能最终超额认购你的CPU,造成延迟或抖动。

Still, you should not try to modify the default options unless you find an actual performance problem. You may end up oversubscribing your CPU and causing delays or thrashing.

这篇关于您可以强制Parallel.Invoke使用多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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