Powershell Throttle Multi thread jobs via job completion

后端 未结 6 1492
情歌与酒
情歌与酒 2020-12-07 03:23

All the tuts I have found use a pre defined sleep time to throttle jobs. I need the throttle to wait until a job is completed before starting a new one. Only 4 jobs can be r

6条回答
  •  自闭症患者
    2020-12-07 03:59

    I wrote a blog article which covers multithreading any given script via actual threads. You can find the full post here:

    http://www.get-blog.com/?p=189

    The basic setup is:

    $ISS = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
    $RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads, $ISS, $Host)
    $RunspacePool.Open()
    
    $Code = [ScriptBlock]::Create($(Get-Content $FileName))
    $PowershellThread = [powershell]::Create().AddScript($Code)
    
    $PowershellThread.RunspacePool = $RunspacePool
    $Handle = $PowershellThread.BeginInvoke()
    $Job = "" | Select-Object Handle, Thread, object
    $Job.Handle = $Handle
    $Job.Thread = $PowershellThread
    $Job.Object = $Object.ToString()
    
    $Job.Thread.EndInvoke($Job.Handle)
    $Job.Thread.Dispose()
    

提交回复
热议问题