question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: More functional and parallel processing functionallity

See original GitHub issue

Summary of the new feature/enhancement

I currently have this block of assignments within my script:

$a = Get-Foo | ConvertTo-Json
$b = Get-Bar
$c = Get-BarFoo
$d = Get-FooBar
$e = Get-FooFoo
$f = Get-BarBar
$g = Get-FooUsing -Parameter $a
Do-SomethingThatChangesGlobalState

where none of the lines depend on the former ones. Currently there is no good way to tell powershell to do all these pipes in parallel and just store the result in the variable.

Proposed technical implementation details (optional)

I think about something like:

#pureFunctions
$a = Get-Foo | ConvertTo-Json
$b = Get-Bar
$c = Get-BarFoo
$d = Get-FooBar
$e = Get-FooFoo
$f = Get-BarBar
$g = Get-FooUsing -Parameter $a
barrier
Do-SomethingThatChangesGlobalState
barrier

Where #pureFunctions tells powershell that the script contains only pure functions and that it is allowed to swap lines as long as lines containing one of the variables as parameter is executed after it is defined. E.g. $g is always executed after $a, but no other guarantee on the execution order needs to be provided and powershell should automatically parallelize the pipelines. For commandlets and functions that do have side effects a barrier-keyword is required, so that they can be used. Optionally also Monads (like in e.g. Haskell) could be implemented to “warp” these.

Ideally, the #pureFunctions declaration should not only work for scripts, but also for scriptblocks, so that a functional entity could easily be integrated into modules and sequential scripts.

Please let me know what you think.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
vexx32commented, Sep 4, 2020

I would… suggest writing it with less duplication:

$yamlJob = {
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]
        $PSPath
    )
    $a = Get-Content -Path $PSPath -Raw | ConvertFrom-Yaml
    @{"getContent" = $a } 
}

$apiJob = {    
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String]
        $xxToken, 
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty]
        [String]
        $xxAPIUrl,

        [Parameter(Mandatory)]
        [scriptblock]
        $ApiAction
    )
    $null = Connect-xxAPI -Token $xxToken -APIurl $xxAPIUrl
    & $ApiAction
}

$xxInventory = @(
    Start-ThreadJob -ScriptBlock $yamlJob -ArgumentList "$PSScriptRoot/file.yaml"
    Start-ThreadJob -ScriptBlock $apiJob -ArgumentList $xxToken, $xxAPIUrl, {
        @{"xxInventoryObjectListOfTypeA" = Get-xxObjectListOfTypeA }
    }
    Start-ThreadJob -ScriptBlock $apiJob -ArgumentList $xxToken, $xxAPIUrl, {
        @{"xxInventoryObjectListOfTypeB" = Get-xxObjectListOfTypeB }
    }
    Start-ThreadJob -ScriptBlock $apiJob -ArgumentList $xxToken, $xxAPIUrl, {
        @{"xxInventoryObjectListOfTypeC" = Get-xxObjectListOfTypeC }
    }
)

Wait-Job -Job $xxInventory

$joinedDictInventory = $xxInventory.Output | ForEach-Object -Begin {[Hashtable]$aa = @{}} -Process {
    foreach($element in ($_.Keys)) {
        $aa.Add($element,$_[$element])
    }
} -End {$aa}
0reactions
SeeminglySciencecommented, Oct 6, 2020

Maybe it’s just me being more optimistic than about this than you are, but I see value for this for libraries and to simplify code of complex operations as well as reducing the time it takes to debug issues with side effects.

I should clarify that my concern is more about the amount of community adoption rather than whether it would be useful to those who did adopt.

I’ve been wrong before though, so if you’re passionate about it I’d recommend drafting an RFC. I can’t really see a way to make these changes without pretty significant rewrites of PowerShell’s architecture, so an implementation proposal would be very helpful to move the conversation forward.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature Request] Parallel processing of multiple ...
I'm facing some problems using the asynchronous functions of OpenFaas, especially when it comes to parallelism of execution with two or more ......
Read more >
parallelism - What is it about functional programming that ...
The main reason is that referential transparency (and even more so laziness) abstracts over the execution order. This makes it trivial to ...
Read more >
Parallel Durable Azure functions
I have a new durable function to replace a long running webjob and it works well and is faster than the former webjob...
Read more >
What Is Parallel Processing? Types and Examples
Parallel processing supports multiple data processing streams through many CPUs working concurrently. Learn how it works, and its top uses.
Read more >
New – Step Functions Support for Dynamic Parallelism
Dynamic parallelism was probably the most requested feature for Step Functions. It unblocks the implementation of new use cases and can help ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found