英文:
Azure Function - Increase Environment.ProcessorCount
问题
我有一个Azure函数,在其中对api端点进行POST调用,我是这样做的:
Parallel.ForEach(activations, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, act =>
    {
        //在此处进行api调用
    });
有一段时间,我在本地运行它,并决定在部署的函数中检查Environment.ProcessorCount的值,结果返回2。
计划是在消耗计划上使用函数。
是否可以增加此变量的值?我尝试添加应用程序密钥FUNCTIONS_WORKER_PROCESS_COUNT,值为4,但结果仍然返回2(说实话,我不知道这个设置是否与Env.ProcessCount相同)。
非常感谢!
英文:
I have an Azure Function in which I am doing a POST call to an api endpoint and I am doing this using:
Parallel.ForEach(activations, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, act =>
    {
        //api call here
    });
For a while, I was running this locally and I decided to check the value of Environment.ProcessorCount in a deployed Function and the value returned was 2.
The plans are to use functions on a Consumption plan.
Is it possible to increase the value of this variable? I tried to add the application key
FUNCTIONS_WORKER_PROCESS_COUNT with a value of 4, but the result still returned 2 (to be honest, I don't know if this setting is the same thing as the Env.ProcessCount).
Many thanks!
答案1
得分: 2
如果您使用 .Net 6 或更高版本,您可以使用 DOTNET_PROCESSOR_COUNT 环境变量来覆盖 .NET 运行时认为的可用处理器数量,以及由 Environment.ProcessorCount 属性报告的数量。
例如,如果您将 DOTNET_PROCESSOR_COUNT 设置为 4,Environment.ProcessorCount 将忽略任何进程关联和 CPU 利用率限制,并返回 4。
(source)
然而,如果您只是想设置 MaxDegreeOfParallelism,为什么不直接这样做:
Parallel.ForEach(activations, 
    new ParallelOptions { MaxDegreeOfParallelism = X }, 
    act =>
    {
        // 在这里进行 API 调用
    });
英文:
If you use .Net 6 or up you can use the DOTNET_PROCESSOR_COUNT environment variable to override the number of processors thought to be available by the .NET runtime and reported by the Environment.ProcessorCount property.
For example, if you set DOTNET_PROCESSOR_COUNT to 4, Environment.ProcessorCount will disregard any process affinity and CPU utilization limit and return 4.
(source)
However, if you do that only to set MaxDegreeOfParallelism, why not just do
Parallel.ForEach(activations, 
    new ParallelOptions { MaxDegreeOfParallelism = X }, 
    act =>
    {
        //api call here
    });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论