性能优势 - 多个 PowerShell 与后台作业

标签 performance powershell

我制作了一个同时启动多个后台作业的程序。目前,该过程需要几个小时(有时需要 3 小时,有时长达 6 小时,具体取决于文件和大小)。

重写代码使其不使用后台作业在同一个 Shell 中启动所有进程,而是为每个单独的进程启动一个 PowerShell 是否有任何优势?或者两者之间没有什么大的区别?

$handler_button1_Click= 
{
    if ($checkBox1.Checked)    { 
    Try{
    $job1 = start-jobhere {& C:\Users\mosermich\Desktop\Extractor\Ressources\adp_staging.bat} -Name "ADP-DATA"
    $listBox1.Items.Add("ADP-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox2.Checked)    { 
    Try{ 
    $job2 = start-jobhere {& C:\Users\mosermich\Desktop\Extractor\Ressources\kdp_staging.bat} -Name "KDP-DATA"
    $listBox1.Items.Add("KDP-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox3.Checked)    { 
    Try{ 
    $job3 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\mdp_staging.bat} -Name "MDP-DATA"
    $listBox1.Items.Add("MDP-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox4.Checked)    { 
    Try{ 
    $job4 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zdlb_staging.bat} -Name "ZDL-B-DATA"
    $listBox1.Items.Add("ZDLB-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox5.Checked)    { 
    Try{ 
    $job5 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zdls_staging.bat} -Name "ZDL-S-DATA"
    $listBox1.Items.Add("ZDLS-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox6.Checked)    { 
    Try{ 
    $job6 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zub_staging.bat} -Name "ZUBOFI-DATA"
    $listBox1.Items.Add("ZUBOFI-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox7.Checked)    { 
    Try{ 
    $job7 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\adp_staging_error.bat} -Name "ADP-ERR"
    $listBox1.Items.Add("ADP-Error-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox8.Checked)    { 
    Try{  
    $job8 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\kdp_staging_error.bat} -Name "KDP-ERR"
    $listBox1.Items.Add("KDP-Error-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox9.Checked)    { 
    Try{  
    $job9 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\mdp_staging_error.bat} -Name "MDP-ERR"
    $listBox1.Items.Add("MDP-Error-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox10.Checked)    { 
    Try{  
    $job10 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zdlb_staging_error.bat} -Name "ZDL-B-ERR"
    $listBox1.Items.Add("ZDL-B-Error-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox11.Checked)    { 
    Try{  
    $job11 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zdls_staging_error.bat} -Name "ZDL-S-ERR"
    $listBox1.Items.Add("ZDL-S-Error-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($checkBox12.Checked)    { 
    Try{  
    $job12 = start-jobhere { & C:\Users\mosermich\Desktop\Extractor\Ressources\zub_staging_error.bat} -Name "ZUBOFI-ERR"
    $listBox1.Items.Add("ZUBOFI-Error-staging läuft...")
    }catch [System.Exception]{}
    }

    if ($listBox1.Items.Count -eq 0) {
    $listBox1.Items.Add("No-Data!") 
    }

    Get-Job | Wait-Job | Where State -eq "Running"


    $listBox1.Items.Add("All Jobs have been succesfully finished")

    }else{}

最佳答案

(我猜)您不会看到很大的区别,因为使用 Start-Job 生成后台作业或使用 -AsJob 调用 comdlet 实际上会创建新的实例PowerShell 可执行文件 - 您可以使用任务管理器/进程资源管理器等查看。

如果您同时创建大量后台作业,那么与它们的实际工作量相比,创建和调度这些作业的开销可能相当大。在这种情况下,您可能需要查看更轻量级的 PowerShell 运行空间。特别是你可以看看优秀的 PoshRSJob使运行空间工作看起来像处理“常规”PowerShell 作业的模块。它还包括对任务进行排队的功能,这样您就不会同时执行 1000 个任务,而只会执行例如8 在任何给定时间。

关于性能优势 - 多个 PowerShell 与后台作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39159389/

相关文章:

powershell - 未指定 Azure PowerShell 证书

arrays - 如何正确优化 MArray 函数以提高速度?

Powershell Rename-Item 一次性替换多个命名参数

python - 如何将 Python 3.3 添加到 Powershell?

powershell - 冒号作为参数中的字符串

powershell - 如何在 native 不支持的powershell参数中使用通配符?

PHP - 具有多个数据库的应用程序

Android 持久缓存 : using ModelCache from ignition

javascript - 在 Typed.js 中的字符串前面获取退格键

javascript - 由于第三方分析脚本,加快了移动设备上加载缓慢的网站速度