我有一个递归函数,执行了大约 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
}
Output:
$job | Receive-Job
Count is: 224
Count is: 225
Count is: 226
Count is: 227
The script failed due to call depth overflow.
在我的机器上,深度溢出始终发生在 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()
}
}
参考:
.net 队列类 https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1?view=net-6.0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)