Demo of PowerShell Pipeline Behavior – Asynchronous vs. Synchronous

When I teach PowerShell classes I sometimes get into a discussion on the more advanced topic (at least for a beginner class) of pipeline behavior in terms of running multiple elements of a pipe synchronously or asynchronously.  I usually have a tough time demo’ing this in a really meaningful way until this week.

The challenge for me was, to make a demo that was very obvious, but have it not include any complication that would take student’s focus off of the demo.  I didn't have a good cmdlet to run that would take a little while but deliver result objects steadily enough so I decided to make one….well a function at least.

First of all, I pre-load this function into my PowerShell prior to the demo:

PS C:\Users\gsiepser> function Get-ProcessSlowly { param([int]$delayinms=300) ; get-process | ForEach-Object {$_ ; Start-Sleep -Milliseconds $delayinms}}

By pre-loading this function, you can keep the confusion of this code away from the students so they can concentrate on the pipeline behavior.  When you want to demo it, just use this like a cmdlet:

PS C:\Users\gsiepser> Get-ProcessSlowly

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName

------- ------ ----- ----- ----- ------ -- -----------

     57 7 1460 4340 53 4524 ApMsgFwd

     75 8 1892 5040 69 0.36 4584 ApntEx

    126 11 2616 8808 81 1.00 3560 Apoint

……

 

I didn’t include the full output, but basically you will see that it outputs a line with a short delay (300ms is the default as you can see in the function).  You can see in the function that you can use a different delay for more or less drama.

PS C:\Users\gsiepser> Get-ProcessSlowly -delayinms 1000

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName

------- ------ ----- ----- ----- ------ -- -----------

     57 7 1460 4340 53 4524 ApMsgFwd

     75 8 1892 5040 69 0.36 4584 ApntEx

    126 11 2616 8808 81 1.06 3560 Apoint

……

 

I tell them that they can imagine any cmdlet/script/function/whatever that takes some time to get its results back to the pipeline.  Then I show them what a “sort-object” or an “-autosize” will do.  It makes this a very obvious demo of how their pipe composition has a major effect on the user experience and timing of the code.

PS C:\Users\gsiepser> Get-ProcessSlowly | Sort-Object ws –Descending

Or

PS C:\Users\gsiepser> Get-ProcessSlowly | Format-Table –AutoSize

Both of these show you that you are stuck waiting until all of the processes are returned prior to being able to see anything in the host.

Anyway, this helped me out a bit and I hope it helps out someone out there.  I thought I would share.

 

P.S.  I am very sorry about how slow I have been posting lately.  If anyone out there is actually reading this blog, send me a nasty-gram email :)

-Gary Siepser

 

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.