且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

管道中的 cmdlet 是否并行执行?

更新时间:2022-10-15 18:12:43

有点对,但完全不是.

我是什么意思?首先,让我们解决您的文档问题.以下内容来自

注意PowerShell通过-OutBuffer公共参数支持外部输出缓冲控制,这也会影响执行流程:

希望这是有道理的!

这是我为上面的演示编写的代码.

以下函数的 Write-Host 输出将根据我们使用的别名改变其颜色,因此在 shell 中更容易区分.

function Test-Pipeline {参数([参数(ValueFromPipeline)][psobject[]]$InputObject)开始 {$WHSplat = @{ForegroundColor = switch($MyInvocation.InvocationName){'第一的' {'绿'}'第二' {'黄色'}'第三' {'红色的'}}}写主机开始 $($MyInvocation.InvocationName)"@WHSplat$对象计数 = 0}过程 {foreach($InputObject 中的 $Object) {$对象计数 += 1写主机在 $($MyInvocation.InvocationName) 中处理对象 #$($ObjectCount)"@WHSplat写输出 $Object}}结尾 {写主机结束 $($MyInvocation.InvocationName)"@WHSplat}}Set-Alias -Name first -Value Test-PipelineSet-Alias -Name second -Value Test-PipelineSet-Alias -Name 第三 -Value Test-Pipeline

I spotted an interesting statement in "PowerShell Notes for professionals" whitepaper - "In a pipeline series each function runs parallel to the others, like parallel threads":

Is that correct? if "yes", is there a technical documentation that supports this statement?

It's kinda true, but not really at all.

What do I mean with that? First, let's get your documentation question out of the way. The following is from paragraph §3.13 of the PowerShell version 3.0 Language Specification:

If a command writes a single object, its successor receives that object and then terminates after writing its own object(s) to its successor. If, however, a command writes multiple objects, they are delivered one at a time to the successor command, which executes once per object. This behavior is called streaming. In stream processing, objects are written along the pipeline as soon as they become available, not when the entire collection has been produced.

When processing a collection, a command can be written such that it can do special processing before the initial element and after the final element.

Now, let's have a brief look at what a cmdlet consists of.


Cmdlets and their building blocks

It may be enticing to think of a cmdlet as just another function, a sequential set of statements to be executed synchronously whenever invoked. This is not correct, however.

A cmdlet, in PowerShell, is an object that implements one of at least 3 methods:

Once a pipeline starts executing, BeginProcessing() is called on every single cmdlet in the pipeline. In this sense, all cmdlets in the pipeline are running "in parallel" - but this design basically allows us to execute the pipeline with a single thread - so actual parallel processing involving multiple threads is not necessary to execute the pipeline as designed.

It's probably more accurate to point out that cmdlets execute concurrently in a pipeline.


Let's try it out!

Since the three methods above maps directly onto the begin, process and end blocks that we can define in an advanced function, it's easy to see the effect of this execution flow.

Let's try and feed 5 objects to a pipeline consisting of three cmdlets reporting their state with Write-Host and see what happens (see code below):

PS C:> 1..5 |first |second |third |Out-Null

Be aware that PowerShell supports external output buffering control via the -OutBuffer common parameter, and this will influence the execution flow as well:

Hope this made some sense!


Here's the code I wrote for the demonstration above.

The Write-Host output from the below function will change its colour based on which alias we use, so it's a little easier to distinguish in the shell.

function Test-Pipeline {
  param(
    [Parameter(ValueFromPipeline)]
    [psobject[]]$InputObject
  )

  begin {
    $WHSplat = @{
      ForegroundColor = switch($MyInvocation.InvocationName){
        'first' {
          'Green'
        }
        'second' {
          'Yellow'
        }
        'third' {
          'Red'
        }
      }
    }
    Write-Host "Begin $($MyInvocation.InvocationName)" @WHSplat
    $ObjectCount = 0
  }

  process {
    foreach($Object in $InputObject) {
      $ObjectCount += 1
      Write-Host "Processing object #$($ObjectCount) in $($MyInvocation.InvocationName)" @WHSplat
      Write-Output $Object
    }
  }

  end {
    Write-Host "End $($MyInvocation.InvocationName)" @WHSplat
  }

}

Set-Alias -Name first  -Value Test-Pipeline
Set-Alias -Name second -Value Test-Pipeline
Set-Alias -Name third  -Value Test-Pipeline