简体   繁体   English

Powershell作业未并行执行

[英]powershell jobs not executed in parallel

Expecting this to run in 5+ seconds (Assuming I have 4 cores and each job can run on each core). 预计此操作将在5秒钟以上运行(假设我有4个内核,并且每个作业都可以在每个内核上运行)。

$emailTimes =  @(0) * 4  

Write-Host "[$(Get-Date -Format HH:mm:ss.fff)] Starting"  
for ($index=0;$index -lt ($emailTimes.length); $index++) {  
  Start-Job -Name "job$index" -ScriptBlock {sleep 5}  
}  
Write-Host "[$(Get-Date -Format HH:mm:ss.fff)] Waiting"  
Get-Job | Wait-Job  
Get-Job | Receive-Job  
Write-Host "[$(Get-Date -Format HH:mm:ss.fff)] Completed"  

Cores do not matter, you can run 100 jobs with one core in parallel. 核心无关紧要,您可以并行运行一个核心的100个作业。

What i did now is: 我现在所做的是:

foreach ($i in 1..10 ) { Start-Job -ScriptBlock {SLEEP -Seconds 5} }
MEASURE-COMMAND { Get-Job|Wait-Job }

and it finishes in: 它完成于:

TotalSeconds      : 6,2208058

Yet - running it few times, once i got: 然而,一旦我得到,运行几次:

TotalSeconds      : 14,3781632

So it can be slow, but it's definitely running in parallel 所以它可能很慢,但是肯定是并行运行的

I've started checking this and I i think OP might be on to something. 我已经开始检查这一点,我认为OP可能正在解决某些问题。 have started few jobs in parallel like this : 并行地开始了一些工作,例如:

    get-job|remove-job
echo "start:"
get-date -UFormat " %H:%M:%S"
1..18| % { 
    Start-Job -ScriptBlock {
        $obj = ""|select starttime, runtime, finishtime 
        $obj.starttime = get-date -UFormat " %H:%M:%S"
        $obj.runtime = measure-command {SLEEP -Seconds 1}|select -ExpandProperty totalseconds
        $obj.finishtime = get-date -UFormat " %H:%M:%S"
        $obj
    } |out-null 

} 
echo "finished spinning up jobs at:"
get-date -UFormat " %H:%M:%S"
echo "all jobs finished at "
Get-Job|Wait-Job |out-null
get-date -UFormat " %H:%M:%S"
echo "run time of each job"

get-job|receive-job| ft

and result was like this output: 结果是这样的输出:

start:
 23:04:27
finished spinning up jobs at:
 23:04:31
all jobs finished at
 23:05:23
run time of each job

starttime   runtime finishtime
---------   ------- ----------
 23:05:07 1,0037849  23:05:08
 23:05:07 1,0037791  23:05:08
 23:05:22 1,0036025  23:05:23
 23:05:22 1,0040943  23:05:23
 23:05:22  1,003943  23:05:23
 23:05:22 1,0116236  23:05:23
 23:05:22 1,0217624  23:05:23
 23:05:22 1,0056518  23:05:23
 23:05:22 1,0038563  23:05:23
 23:05:22 1,0082825  23:05:23
 23:05:22 1,0042454  23:05:23
 23:05:22 1,0033923  23:05:23
 23:05:22 1,0128473  23:05:23
 23:05:22 1,0190476  23:05:23
 23:05:22 1,0188348  23:05:23
 23:05:22 1,0235496  23:05:23
 23:05:22 1,0058239  23:05:23
 23:05:22 1,0153528  23:05:23

Notice 40 second delay from script start to first job start, and 15 second break between first 2 and rest? 请注意,从脚本开始到第一次作业开始需要40秒的延迟,而在开始2和休息之间要间隔15秒?

The more jobs run in parallel - the worse it gets. 并行运行的作业越多-越差。 Starting 100 jobs with 10 second sleep (so that first does not finish before all are started) just hung PS window and never finished... which is weird, because i was using hundreds of jobs , each taking more than 10 seconds to finish and have never encountered such issues before. 以10秒钟的睡眠来启动100个工作(这样,在开始所有工作之前就不会完成)只是挂了PS窗口而从未完成...这很奇怪,因为我正在使用数百个工作,每个工作都需要10秒钟以上才能完成,以前从未遇到过此类问题。

This needs further testing of different machines (os/processor/anti-virus maybe?) and different powershell versions. 这需要对不同的机器(可能是OS /处理器/防病毒软件)和不同的Powershell版本进行进一步测试。

I also thought that maybe it's something specific to start-sleep function but no - have used different methods to get it running few seconds and still same effect. 我还认为,也许这是启动睡眠功能所特有的,但不是-使用不同的方法使它运行几秒钟并仍然具有相同的效果。 Have to check it because I use powershell jobs (mainly invoke-command -asjob ) daily and if this keeps like this - I might need to consider switching to runspaces or workflows for my parallel processing needs... 必须检查一下,因为我每天都使用Powershell作业(主要是invoke-command -asjob),如果这种情况一直如此-我可能需要考虑切换到运行空间或工作流以满足我的并行处理需求...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM