Is this a sensible way to share functions with ForEach-Object -Parallel?

0
0
Asked By MellowCedar42 On

I'm experimenting with the -Parallel parameter in PowerShell 7's ForEach-Object. My current approach serializes several functions and an output path into a hashtable, passes that data into each parallel runspace with $Using:, recreates the functions and variables there, and then processes directory objects from Get-SubDirStream. I limit concurrency to four operations so the CPU isn't overwhelmed. Is this a reasonable pattern, or is there a cleaner or more efficient way to make functions and shared values available to each parallel task?

1 Answer

Answered By BrightHarbor7 On

That approach is reasonable for starting thread-based work. One way to make it cleaner is to store the functions in an ordered hashtable using keys such as "function:AddData", then loop over the imported values inside the parallel script block and assign them through the session state. That keeps the setup code short even when you need to pass many functions or variables. You can also avoid declaring $params as an empty hashtable and immediately replacing it; just create it when you build the parameter set. A throttle limit of four is fairly conservative, but leaving a couple of the VM's six cores available for the operating system and other applications is a sensible choice.

MellowCedar42 -

I intentionally declare $params first because the explicit hashtable type gives my editor useful completion and type hints. The limit of four is also deliberate since the VM has six cores and I want to leave some capacity for the OS and background work.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.