本文介绍了使用Powershell作业,运行空间或工作流时,线程是否在单独的内核上执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Powershell作业,运行空间或工作流时,线程是否在单独的内核上执行? (如果是这样,我们如何告诉Powershell使用多少个内核?–抱歉,这是两个问题.)

When using Powershell Jobs, Runspaces, or Workflows, are the threads being executed on separate cores? (and if so, how do we tell powershell how many cores to use? -- sorry that's 2 questions.)

.Net具有任务并行库,该库允许使用所有可用内核并行运行"for循环"(). Powershell作业,运行空间或工作流是否执行类似的操作?同样,我的意思是线程实际上是在并行的独立内核上运行的吗?

.Net has the Task Parallel Library, which allows a 'for loop' to run in parallel, using all available cores (here is one example). Does Powershell Jobs, Runspaces, or Workflows do something similar? And by similar, I mean are the threads actually running on separate cores, in parallel?

我在此处找到了类似的问题,但似乎不清楚(无论如何还是我)如果线程是在单独的内核上执行的.似乎有时将多线程误认为是并行的,如此处.

I found a similar question here, but it seems unclear (to me anyway) if the threads are executed on separate cores. It seems that sometimes multi-threads are mistaken for parallel, as mentioned here.

如果无法在Powershell中使用多个内核,我将使用C#或Python,但是我的首选是使用Powershell,因为(插入一长串原因).

If using multiple cores with Powershell is not possible, I will use C# or perhaps Python, but my first choice is to use Powershell because (insert long list of reasons).

如果有帮助,我正在尝试做所有这些事情来帮助从事服务器管理员工作的同事.当前,他的powershell脚本遍历服务器列表,而foreach服务器可以完成工作.当前,该脚本在8核计算机上运行,​​但仅使用一个核.我敢肯定还有其他提高性能的方法,但是并行执行是我当前的目标.

If it's relevant, I'm trying to do all this to help a co-worker who does server admin type stuff. Currently, his powershell script loops through a list of servers, and foreach server, does stuff. Currently the script runs on an 8 core machine, but is only using one core. I'm sure there are other ways to boost performance, but doing it in parallel is my current goal.

推荐答案

如果通过单独的内核,您的意思是逻辑内核(不是物理内核),我会说是.运行类似的内容,并注意"$ num"的输出. $ num表示当前正在其上运行线程的逻辑(非物理)内核.如果此脚本在双核计算机(具有4个逻辑核)上运行,则$ num的输出为0、1、2或3.请参见此处,以获取有关逻辑CPU和物理CPU的更好解释.

I would say yes if by separate cores, you mean logical cores (not physical cores). Run something like this and notice the output from "$num". $num represents the current logical (not physical) core on which a thread is running. If this script runs on a dual core machine (with 4 logical cores), the output of $num is a 0, 1, 2, or 3. See here for a better explanation on Logical vs Physical CPU.

$sb = {
    param($sbarg)
$MethodDefinition = @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern int GetCurrentProcessorNumber();
'@

    $Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru

    0..10 | % {
    $num = $Kernel32::GetCurrentProcessorNumber() 
    Write-Output "[$sbarg]:[$_] on '$num'"

    # simulate some work that may make the cpu busy
    # perhaps watch in task manager as well to see load
    $result = 1; foreach ($number in 1..1000000) {$result = $result * $number};

    Start-Sleep -Milliseconds 500
    }
}

0..10 | % {
    Start-Job -ScriptBlock $sb -ArgumentList $_
}

Get-Job

# Wait for it all to complete
While (Get-Job -State "Running")
{
    Write-Output "waiting..."
    Start-Sleep 2
}

# Getting the information back from the jobs
Get-Job | Receive-Job

# Clean Up
Get-Job | Remove-Job

这篇关于使用Powershell作业,运行空间或工作流时,线程是否在单独的内核上执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:06