首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并行化powershell脚本执行

并行化powershell脚本执行
EN

Stack Overflow用户
提问于 2012-02-03 05:56:56
回答 3查看 1.8K关注 0票数 1

我有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脚本,它们之间的依赖关系也可能增加。

是否有任何一个通过找到依赖来实现并行性?如果是的话,请告诉我如何进行。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-03 18:20:07

一般来说,你对并行编程有多熟悉?你听说过并使用过互斥这个概念吗?一般的概念是使用某种消息传递/锁定机制来保护不同并行线程之间的共享资源。

在您的例子中,您正在使分界线成为脚本本身--我认为这可能比维基百科文章中概述的大多数技术简单得多。这个简单的模板对你想要的东西有用吗?

  1. 在本地文件系统中定义文件夹。所有脚本都将知道这个位置(默认参数)。
  2. 在运行任何脚本之前,请确保删除该目录中的任何文件。
  3. 对于每个脚本,作为它们执行的最后一步,它们应该在共享目录中写入一个文件,其脚本名作为文件的名称。例如,script1.ps1将创建script1文件。
  4. 任何依赖于另一个脚本的脚本都将根据脚本的文件名定义这些依赖项。如果script3依赖于script1和script2,这将被定义为script3中的依赖参数。
  5. 所有具有依赖关系的脚本都将运行一个函数,该函数将检查它所依赖的脚本是否存在这些文件。如果是,则继续执行脚本,否则会暂停直到它们完成。
  6. 所有脚本都由主脚本/批处理文件同时启动。所有脚本都以PowerShell作业的形式运行,因此操作系统将并行运行它们的执行。大多数脚本都会启动,看到它们有依赖关系,然后耐心地等待它们得到解决,然后继续执行脚本体。

好消息是,这将允许灵活地更改依赖项。每个脚本都会编写一个文件,对其他人是否在等待它们没有做出任何假设。更改特定脚本的依赖性将是输入参数的简单一行更改或更改。

不过,这绝对不是一个完美的解决方案。例如,如果脚本失败(或者您的脚本可以退出多个不同的代码路径,但您忘记在其中一个路径中写入文件),会发生什么情况?这可能导致死锁的情况,没有依赖的脚本将被启动。另一件不好的事情是,在等待创建正确的文件时,忙于等待睡眠或旋转--这可以通过实现基于事件的方法来纠正,在这种方法中,可以让操作系统监视目录的变化。

希望这能帮上忙,而不是全是垃圾。

票数 2
EN

Stack Overflow用户

发布于 2012-02-03 07:46:32

您需要查看PowerShell 3.0工作流。它提供了您的需求所需的功能。就像这样:

代码语言:javascript
复制
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"
            }
        }
    }
} 
票数 5
EN

Stack Overflow用户

发布于 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9124308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档