Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3989,6 +3989,9 @@ function Get-HarnessUnguardedCallbacks {
$flagPattern = [regex]::Escape($flag) + '(?![\w:])'
$statement = $loop
if ($loop.Parent -is [System.Management.Automation.Language.AssignmentStatementAst]) { $statement = $loop.Parent }
# `$x = @(do { ... } while ($false))`, which counts the values the
# callback output (-GroupProvider's call).
elseif ($loop.Parent.Parent -is [System.Management.Automation.Language.ArrayExpressionAst] -and $loop.Parent.Statements.Count -eq 1 -and $loop.Parent.Parent.Parent.Parent -is [System.Management.Automation.Language.AssignmentStatementAst]) { $statement = $loop.Parent.Parent.Parent.Parent }
$siblings = $statement.Parent.Statements
$index = $siblings.IndexOf($statement)
# The last statement before the call that mentions the flag resets it.
Expand Down Expand Up @@ -4420,6 +4423,44 @@ $null = Invoke-PkProcessDispatch -MaxConcurrent 1 `
-JobFinalize { param($job, $pending) }
Test-Assert ($glSeen.Count -eq 1 -and $glSeen[0] -is [array] -and ($glSeen[0] -join ',') -eq 'a,b,c' -and $script:messages.Count -eq 0) ("-GroupProvider returning ,@('a', 'b', 'c') gives one group holding the list, without a warning (" + $glSeen.Count + " group(s))")

# -GroupProvider outputting several values in one call: they're one group, as
# without the comma, and the run warns once, with how many arrived. One value,
# $null, nothing, and a list returned with a comma (one value, even holding one
# item) aren't warned about. Each provider gives two groups, if it gives any.
$gvCases = [ordered]@{
'one value' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return ('g' + $gv.Calls) }; Groups = 'g1|g2'; Count = 0 }
'$null' = @{ Provider = { return $null }; Groups = ''; Count = 0 }
'nothing' = @{ Provider = { }; Groups = ''; Count = 0 }
'a list with the comma' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return ,@('a', 'b') }; Groups = '[a,b]|[a,b]'; Count = 0 }
'a one-item list, comma' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return ,@('a') }; Groups = '[a]|[a]'; Count = 0 }
'a stray value and the group' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; $gv.Log.Add('x'); return ('g' + $gv.Calls) }; Groups = '[0,g1]|[1,g2]'; Count = 2 }
'a list without the comma' = @{ Provider = { if ($gv.Calls++ -ge 2) { return $null }; return @('a', 'b', 'c') }; Groups = '[a,b,c]|[a,b,c]'; Count = 3 }
}
foreach ($gvName in $gvCases.Keys) {
Reset-Harness
$gvCase = $gvCases[$gvName]
$gv = @{ Calls = 0; Log = [System.Collections.ArrayList]::new() }
$gvSeen = [System.Collections.Generic.List[string]]::new()
$null = Invoke-PkProcessDispatch -MaxConcurrent 1 -GroupProvider $gvCase.Provider.GetNewClosure() `
-JobBuilder { param($g) if ($g -is [array]) { $gvSeen.Add('[' + ($g -join ',') + ']') } else { $gvSeen.Add([string]$g) }; return @() }.GetNewClosure() `
-JobFinalize { param($job, $pending) }
$gvWarnings = @($script:messages | Where-Object { $_.Level -eq 'Warn' })
$gvText = ($gvWarnings | ForEach-Object { $_.Message }) -join ' / '
$gvOk = (($gvSeen -join '|') -eq $gvCase.Groups)
if ($gvCase.Count -eq 0) { $gvOk = $gvOk -and $gvWarnings.Count -eq 0 }
else { $gvOk = $gvOk -and $gvWarnings.Count -eq 1 -and $gvText -like ('*-GroupProvider output ' + $gvCase.Count + ' values*') -and $gvText -like '*$null = ...*' -and $gvText -like '*return ,$list*' }
Test-Assert $gvOk ("-GroupProvider giving " + $gvName + ": groups " + ($gvSeen -join '|') + ", " + $gvWarnings.Count + " warning(s)" + $(if ($gvText) { ': ' + $gvText }))
}

# A group value keeps what PowerShell added to it: a note property on a string.
Reset-Harness
$gn = @{ Given = $false; Tag = 'not seen' }
$null = Invoke-PkProcessDispatch -MaxConcurrent 1 `
-GroupProvider { if ($gn.Given) { return $null }; $gn.Given = $true; return ('a' | Add-Member -NotePropertyName Tag -NotePropertyValue 'pk-tag' -PassThru) }.GetNewClosure() `
-JobBuilder { param($g) $gn.Tag = [string]$g.Tag; return @() }.GetNewClosure() `
-JobFinalize { param($job, $pending) }
Test-Assert ($gn.Tag -eq 'pk-tag') ("a group value keeps a note property PowerShell added to it (Tag: " + $gn.Tag + ")")

# -NextJobProvider: only the last value decides. Two hashtables run the
# second, with a warning about the first; a hashtable let out before
# `return $null` isn't run: the run ends, with a warning.
Expand Down
33 changes: 29 additions & 4 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,8 @@ function Invoke-PkCallback {
# only the callback, like `return`. A labeled `break outer` still reaches
# the caller's loop. Inlined where a callback runs per job or per result
# (on 5.1 this call more than doubled the dispatcher's own time per job),
# and in Invoke-PkMessageHandler.
# in Invoke-PkMessageHandler, and for -GroupProvider in
# Invoke-PkProcessDispatchCore, which counts the values it outputs.
# pk-prefixed: in a worker, a callback without .GetNewClosure() sees this frame.
param(
[Parameter(Mandatory)][scriptblock]$PkCallback,
Expand Down Expand Up @@ -2347,8 +2348,9 @@ function Invoke-PkProcessDispatch {
# .PARAMETER GroupProvider
# { } returning the next group, or $null when there are no more, and
# outputting nothing else. Return a list as one group with a comma:
# return ,@('a', 'b'). Output ending in $null is taken as the end, with a
# warning, so a list group can't end in $null.
# return ,@('a', 'b'). Several values output in one call are taken
# together as one group, with a warning. Output ending in $null is taken
# as the end, with a warning, so a list group can't end in $null.
# .PARAMETER JobBuilder
# { param($group) } returning the group's job hashtable(s), or @() for none.
# .PARAMETER JobStarter
Expand Down Expand Up @@ -2683,6 +2685,9 @@ function Invoke-PkProcessDispatchCore {
# Invoke-PkCallback's -PkContext: callbacks that used continue/break are
# warned about once each, under this prefix.
$pkCallbackContext = @{ Prefix = 'ProcessDispatch: '; Warned = @{} }
# -GroupProvider outputting several values in one call is warned about
# once per run.
$pkProviderWarned = $false
# Teardown is in a finally, guarded by this flag: when PowerShell stops
# the script (Ctrl+C outside Invoke-PkRun, the runspace engine
# force-stopping a worker), catch blocks are skipped.
Expand All @@ -2703,7 +2708,27 @@ function Invoke-PkProcessDispatchCore {
}

while ((-not $pkProviderExhausted) -and $pkActiveGroupCount -lt $pkMaxQueueAhead -and (-not $pkCancelToken.IsCancellationRequested)) {
$pkGroupInfo = Invoke-PkCallback -PkCallback $pkGroupProvider -PkName '-GroupProvider' -PkContext $pkCallbackContext
# Invoke-PkCallback inlined, to count the values the provider
# output. Collected by plain assignment, `return ,@('a', 'b')` (one
# value, a list) and two values output separately are the same
# array, and only the second is a mistake. @() keeps one value per
# element, each as it was output (a ForEach-Object or a .NET
# collection's Add() would change a value's PSObject wrapper).
$pkCbDone = $false
$pkProvided = @(do { & $pkGroupProvider; $pkCbDone = $true } while ($false))
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-GroupProvider' -PkContext $pkCallbackContext }
# The group is what assignment gives: one value as itself, several
# as one array (unchanged; it only gets a warning now).
$pkGroupInfo = $null
if ($pkProvided.Count -eq 1) { $pkGroupInfo = $pkProvided[0] }
elseif ($pkProvided.Count -gt 1) {
$pkGroupInfo = $pkProvided
# Output ending in $null has its own warning, below.
if (-not $pkProviderWarned -and $null -ne $pkProvided[-1]) {
$pkProviderWarned = $true
Write-PkMessage ("ProcessDispatch: -GroupProvider output " + $pkProvided.Count + " values in one call, taken together as one group. Output one value per call: `$null = ... discards a value you don't want (an ArrayList.Add index), and return ,`$list returns a list as one group. Shown once per run.") -Level Warn
}
}
# A value the provider leaks before `return $null` (an ArrayList.Add
# index) arrives as @(<index>, $null). Taken as one more group, the
# dispatcher would ask again forever, so output ending in $null is
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ All of these callbacks run on whichever thread called the dispatcher: the main t
- **`-GroupCompleteCallback` also runs for a group an abort cut short**, so you can record the jobs in it that did finish. Before doing anything irreversible there (zipping the folder, deleting sources), check `Test-PkCancelRequested` (or your own `-CancelToken`) and skip it if true.
- Groups are pulled lazily, at most `-MaxQueueAhead` (default 3) ahead.
- A group can be any value: a hashtable, a path, a name. Each job gets its group as `$job.Group`.
- Each `-GroupProvider` call outputs exactly one group, or `$null` to end, and nothing else: `return $group`, `return ,@('a', 'b', 'c')` for a list as one group (the comma keeps a one-item list a list), or `return $null`. Anything else it lets out (an `ArrayList.Add` index, say) becomes part of the group, so discard it with `$null = ...`. Output that ends in `$null` is taken as the end, with a warning, so a list group can't end in `$null`.
- Each `-GroupProvider` call outputs exactly one group, or `$null` to end, and nothing else: `return $group`, `return ,@('a', 'b', 'c')` for a list as one group (the comma keeps a one-item list a list), or `return $null`. Anything else it lets out (an `ArrayList.Add` index, say) becomes part of the group, so discard it with `$null = ...`. When a call outputs more than one value (a stray value, or a list returned without the comma), the dispatcher warns once per run, with how many values arrived; the group is the same as without the warning. Output that ends in `$null` is taken as the end, with a warning, so a list group can't end in `$null`.
- `-GroupReleaseCheck { param($group) }` makes a group with zero jobs keep its queue-ahead slot until the check returns true. Use it to stop one dispatcher racing ahead of another that it's paired with. The dispatcher doesn't return until every held group has been released, unless the run is canceled or aborted. It runs on every loop pass, so keep it cheap. Only its last output value counts, so a stray value written before the answer doesn't release the group.

</details>
Expand Down
Loading