我有一个递归函数,执行了大约 750 次 - 迭代 XML 文件并进行处理。代码正在运行使用Start-Job
下面的例子:
$job = Start-Job -ScriptBlock {
function Test-Function {
Param
(
$count
)
Write-Host "Count is: $count"
$count++
Test-Function -count $count
}
Test-Function -count 1
}
Run Code Online (Sandbox Code Playgroud)
输出:
$job | Receive-Job
Count is: 224
Count is: 225
Count is: 226
Count is: 227
The script failed due to call depth overflow.
Run Code Online (Sandbox Code Playgroud)
在我的机器上,深度溢出始终发生在 227。如果我删除Start-Job
,我可以达到 750~(甚至更高)。我正在使用作业进行批处理。
使用时有没有办法配置深度溢出值Start-Job
?
这是 PowerShell 作业的限制吗?
我无法回答 PS 5.1 / 7.2 中调用深度溢出限制的具体细节,但您可以基于作业中的队列进行递归。
因此,您不是在函数内进行递归,而是从外部进行递归(尽管仍在作业内)。
这就是它的样子。
$job = Start-Job -ScriptBlock {
$Queue = [System.Collections.Queue]::new()
function Test-Function {
Param
(
$count
)
Write-Host "Count is: $count"
$count++
# Next item to process.
$Queue.Enqueue($Count)
}
# Call the function once
Test-Function -count 1
# Process the queue
while ($Queue.Count -gt 0) {
Test-Function -count $Queue.Dequeue()
}
}
Run Code Online (Sandbox Code Playgroud)
参考:
归档时间: |
|
查看次数: |
141 次 |
最近记录: |