码迷,mamicode.com
首页 > 其他好文 > 详细

并行执行

时间:2014-05-29 14:12:57      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:c   tar   a   com   art   os   

$throttleLimit = 4
$SessionState = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $SessionState, $Host)
$Pool.Open()

$id = 3
$ScriptBlock = {
#此处参数顺序要注意前后,AddArgument的第一个参数会被传递给$id,AddArgument的第二个参数会被传递给$x,与AddArgument的顺序无关,只与param的顺序有关
param($id,$x)

Start-Sleep -Seconds 1
"Done processing ID $id"
"hello, $x"
}

$threads = @()

$handles = for ($x = 1; $x -le 20; $x++) {
$powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($id).addargument($x)
$powershell.RunspacePool = $Pool
$powershell.BeginInvoke()
$threads += $powershell
}

do {
$i = 0
$done = $true
foreach ($handle in $handles) {
if ($handle -ne $null) {
if ($handle.IsCompleted) {
$threads[$i].EndInvoke($handle)
$threads[$i].Dispose()
$handles[$i] = $null
} else {
$done = $false
}
}
$i++
}
if (-not $done) { Start-Sleep -Milliseconds 500 }
} until ($done)

并行执行,布布扣,bubuko.com

并行执行

标签:c   tar   a   com   art   os   

原文地址:http://www.cnblogs.com/dreamer-fish/p/3756450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!