我有8个powershell脚本。它们中很少有依赖关系。这意味着他们不能并行执行。他们应该一个接一个地被处决。
一些Powershell脚本没有依赖性,可以并行执行。
下面是详细解释的依赖项
Powershell scripts 1, 2, and 3 depend on nothing else Powershell script 4 depends on Powershell script 1 Powershell script 5 depends on Powershell scripts 1, 2, and 3 Powershell script 6 depends on Powershell scripts 3 and 4 Powershell script 7 depends on Powershell scripts 5 and 6 Powershell script 8 depends on Powershell script 5
我知道,通过手动硬编码,依赖是可能的。但是,还可以添加10个powershell脚本,它们之间的依赖关系也可能增加。
是否有任何一个通过找到依赖来实现并行性?如果是的话,请告诉我如何进行。
发布于 2012-02-03 18:20:07
一般来说,你对并行编程有多熟悉?你听说过并使用过互斥这个概念吗?一般的概念是使用某种消息传递/锁定机制来保护不同并行线程之间的共享资源。
在您的例子中,您正在使分界线成为脚本本身--我认为这可能比维基百科文章中概述的大多数技术简单得多。这个简单的模板对你想要的东西有用吗?
好消息是,这将允许灵活地更改依赖项。每个脚本都会编写一个文件,对其他人是否在等待它们没有做出任何假设。更改特定脚本的依赖性将是输入参数的简单一行更改或更改。
不过,这绝对不是一个完美的解决方案。例如,如果脚本失败(或者您的脚本可以退出多个不同的代码路径,但您忘记在其中一个路径中写入文件),会发生什么情况?这可能导致死锁的情况,没有依赖的脚本将被启动。另一件不好的事情是,在等待创建正确的文件时,忙于等待睡眠或旋转--这可以通过实现基于事件的方法来纠正,在这种方法中,可以让操作系统监视目录的变化。
希望这能帮上忙,而不是全是垃圾。
发布于 2012-02-03 07:46:32
您需要查看PowerShell 3.0工作流。它提供了您的需求所需的功能。就像这样:
workflow Install-myApp {
param ([string[]]$computername)
foreach -parallel($computer in $computername) {
"Installing MyApp on $computer"
#Code for invoking installer here
#This can take as long as 30mins and may reboot a couple of times
}
}
workflow Install-MyApp2{
param ([string[]]$computername)
foreach -parallel($computer in $computername) {
"Installing MyApp2 on $computer"
#Code for invoking installer here
#This can take as long as 30mins!
}
}
WorkFlow New-SPFarm {
Sequence {
Parallel {
Install-MyApp2 -computername "Server2","Server3"
Install-MyApp -computername "Server1","Server4","Server5"
}
Sequence {
#This activity can happen only after the set of activities in the above parallel block are complete"
"Configuring First Server in the Farm [Server1]"
#The following foreach should take place only after the above activity is complete and that is why we have it in a sequence
foreach -parallel($computer in $computername) {
"Configuring SharePoint on $computer"
}
}
}
} 发布于 2012-02-03 06:11:35
你只需要适当地订购你的电话就行了。没有任何内置的东西可以为您处理依赖关系。
同时运行1,2,3 Start-Job。
等待他们完成Get-Job -State Running | Wait-Job
同时运行4,5 Start-Job
等待他们完成Get-Job -State Running | Wait-Job
跑6然后等它。
同时运行7,8 Start-Job
https://stackoverflow.com/questions/9124308
复制相似问题