PowerShell工作流线程限制
我有一个PowerShell工作流,看起来像这样,但每个Foo函数的功能通常都非常不同:PowerShell工作流线程限制,powershell,workflow,powershell-4.0,Powershell,Workflow,Powershell 4.0,我有一个PowerShell工作流,看起来像这样,但每个Foo函数的功能通常都非常不同: function Foo1 { "Foo1 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date) Start-Sleep 2 } function Foo2 { "Foo2 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date) Start-Sleep 2 } function Foo3 { "Foo3 : {0:hh}:
function Foo1
{
"Foo1 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo2
{
"Foo2 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo3
{
"Foo3 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo4
{
"Foo4 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo5
{
"Foo5 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo6
{
"Foo6 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
workflow Invoke-Workflow
{
parallel
{
Foo1
Foo2
Foo3
Foo4
Foo5
Foo6
}
}
Invoke-Workflow
其输出为:
Foo1:10:28:43
Foo2:10:28:43
Foo3:10:28:44
Foo5:10:28:44
Foo4:10:28:44
Foo6:10:28:46
显示前5项立即运行,然后第6项必须等待前一项完成
我看到了很多说明如何增加foreach循环中并行执行次数的文档。但是,如何增加将在并行块中运行的项目数?似乎没有办法增加将在并行块中使用的线程数,或者如果有,很难找到。解决方法是使用Start Job命令。你可以用
Start Job-Scriptblock{#在这里做事}
或者,如果代码比您希望的内联代码多,您可以让它运行脚本:
启动作业-文件路径“c:\temp\Job.ps1”-参数列表1
每项工作都需要一段时间才能启动,因此它们不会同时启动。我运行了一个循环来运行Start Job命令并记录它们的启动时间。每个作业都有30秒的睡眠时间,这样它就不会立即死亡(并尝试强制运行的作业数量达到线程的限制)。结果略有不同,但测试多达20次表明,它至少能够同时运行那么多次。在下面的结果中,有些是在第一次启动后28秒启动的,但请记住,由于30秒的睡眠,第一次启动仍在运行。所以他们并没有全部发动快速射击,但同时有20人在奔跑
#1 started at 5/29/2015 2:17:25 PM
#2 started at 5/29/2015 2:17:25 PM
#3 started at 5/29/2015 2:17:26 PM
#4 started at 5/29/2015 2:17:26 PM
#5 started at 5/29/2015 2:17:27 PM
#7 started at 5/29/2015 2:17:28 PM
#6 started at 5/29/2015 2:17:28 PM
#8 started at 5/29/2015 2:17:31 PM
#9 started at 5/29/2015 2:17:37 PM
#10 started at 5/29/2015 2:17:47 PM
#12 started at 5/29/2015 2:17:53 PM
#13 started at 5/29/2015 2:17:53 PM
#15 started at 5/29/2015 2:17:53 PM
#18 started at 5/29/2015 2:17:53 PM
#11 started at 5/29/2015 2:17:53 PM
#20 started at 5/29/2015 2:17:53 PM
#14 started at 5/29/2015 2:17:53 PM
#16 started at 5/29/2015 2:17:53 PM
#17 started at 5/29/2015 2:17:53 PM
#19 started at 5/29/2015 2:17:53 PM
我确实在PowerShell中遇到了其他几种多线程方法,但这是最容易理解和演示的方法。这可能不是最好的