How does threading in powershell work?

前端 未结 6 1421
有刺的猬
有刺的猬 2020-12-23 23:14

I want to parallelize some file-parsing actions with network activity in powershell. Quick google for it, start-thread looked like a solution, but:

T

相关标签:
6条回答
  • 2020-12-23 23:21

    The thing that comes closest to threads and is way more performant than jobs is PowerShell runspaces.

    Here is a very basic example:

    # the number of threads
    $count = 10
    
    # the pool will manage the parallel execution
    $pool = [RunspaceFactory]::CreateRunspacePool(1, $count)
    
    try {    
        $pool.Open()
    
        # create and run the jobs to be run in parallel
        $jobs = New-Object object[] $count
        for ($i = 0; $i -lt $count; $i++) {
            $ps = [PowerShell]::Create()
            $ps.RunspacePool = $pool
    
            # add the script block to run
            [void]$ps.AddScript({
                param($Index)
                Write-Output "Index: $index"
            })
    
            # optional: add parameters
            [void]$ps.AddParameter("Index", $i)
    
            # start async execution
            $jobs[$i] = [PSCustomObject]@{
                PowerShell = $ps
                AsyncResult = $ps.BeginInvoke()
            }
        }
        foreach ($job in $jobs) {
            try {
                # wait for completion
                [void]$job.AsyncResult.AsyncWaitHandle.WaitOne()
    
                # get results
                $job.PowerShell.EndInvoke($job.AsyncResult)
            }
            finally {
                $job.PowerShell.Dispose()
            }
        }
    }
    finally {
        $pool.Dispose()
    }
    

    Beyond that, you can do more advanced things like throttle the number of parallel runspaces on the pool, or import functions and variables from the current session etc.

    0 讨论(0)
  • 2020-12-23 23:23

    I have a blog article on how you can multithread any given powershell script.

    Multithreading with Jobs in PowerShell

    Enjoy!

    0 讨论(0)
  • 2020-12-23 23:27

    The answer, now, is quite simple.

    Install-Module -Name ThreadJob -Confirm:$true
    
    $j1= Start-ThreadJob  `
                    -FilePath $YourThreadJob `
                    -ArgumentList @("A","B")
    $j1|get-job
    $j1|receive-job
    
    0 讨论(0)
  • 2020-12-23 23:28

    Powershell does not have a built-in command named Start-Thread.

    V2.0 does, however, have PowerShell jobs, which can run in the background, and can be considered the equivalent of a thread. You have the following commands at your disposal for working with jobs:

    Name                              Category  Synopsis
    ----                              --------  --------
    Start-Job                         Cmdlet    Starts a Windows PowerShell background job.
    Get-Job                           Cmdlet    Gets Windows PowerShell background jobs that are running in the current ...
    Receive-Job                       Cmdlet    Gets the results of the Windows PowerShell background jobs in the curren...
    Stop-Job                          Cmdlet    Stops a Windows PowerShell background job.
    Wait-Job                          Cmdlet    Suppresses the command prompt until one or all of the Windows PowerShell...
    Remove-Job                        Cmdlet    Deletes a Windows PowerShell background job.
    

    Here is an example on how to work with it. To start a job, use start-job and pass a script block which contains the code you want run asynchronously:

    $job  = start-job { get-childitem . -recurse }
    

    This command will start a job, that gets all children under the current directory recursively, and you will be returned to the command line immediately.

    You can examine the $job variable to see if the job has finished, etc. If you want to wait for a job to finish, use:

    wait-job $job
    

    Finally, to receive the results from a job, use:

    receive-job $job
    
    0 讨论(0)
  • 2020-12-23 23:43

    You can't use threads directly like this, but you can't be blamed for trying since once the whole BCL is lying in front of you it's not entirely silly to expect most of it to work :)

    PowerShell runs scriptblocks in pipelines which in turn require runspaces to execute them. I blogged about how to roll your own MT scripts some time ago for v2 ctp3, but the technique (and API) is still the same. The main tools are the [runspacefactory] and [powershell] types. Take a look here:

    http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx

    The above is the most lightweight way to approach MT scripting. There is background job support in v2 by way of start-job, get-job but I figured you already spotted that and saw that they are fairly heavyweight.

    0 讨论(0)
  • 2020-12-23 23:43

    There is an article about background jobs in powershell on MSDN at http://msdn.microsoft.com/en-us/library/dd878288(VS.85).aspx.

    You can also read about jobs in powershell itself.

    > help about_Jobs
    
    0 讨论(0)
提交回复
热议问题