重复使用2个启动线程函数

发布于 2025-01-21 11:04:29 字数 616 浏览 0 评论 0原文

假设get-fooget-foo2deploy-jobs是3个功能,是一个非常大的模块的一部分。我想使用get-fooget-foo2deploy-jobs's start-threadjob(下面)无需每次重新加载整个模块。

是否可以为此做一个工作示例吗?

function Deploy-Jobs {

    foreach ($Device in $Devices) {

        Start-ThreadJob -Name $Device -ThrottleLimit 50 -InitializationScript $initScript -ScriptBlock {
            param($Device)

            Get-Foo | Get-Foo2 -List
        
        } -ArgumentList $Device | out-null
    }
}

Assuming Get-Foo and Get-Foo2 and Deploy-Jobs are 3 functions that are part of a very large module. I would like to use Get-Foo and Get-Foo2 in Deploy-Jobs's Start-ThreadJob (below) without reloading the entire module each time.

Is an working example available for how to do this?

function Deploy-Jobs {

    foreach ($Device in $Devices) {

        Start-ThreadJob -Name $Device -ThrottleLimit 50 -InitializationScript $initScript -ScriptBlock {
            param($Device)

            Get-Foo | Get-Foo2 -List
        
        } -ArgumentList $Device | out-null
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2025-01-28 11:04:29

对于Invoke-Command(psRemoting),start-jobstart-thare-threadJob,您可以使用将函数定义传递给其他范围的方法相同foreeach -object -parallel。由于您想在作业的脚本块中调用2个不同的功能,我认为-Initializationscript是一个选项,即使是,它也可能使代码成为代码甚至比应该复杂得多。

您可以将其用作如何存储2个函数定义的示例在上述范围中,每项工作都将使用。

function Say-Hello {
    "Hello world!"
}

function From-ThreadJob {
    param($i)
    "From ThreadJob # $i"
}

$def = @(
    ${function:Say-Hello}.ToString()
    ${function:From-ThreadJob}.ToString()
)

function Run-Jobs {
    param($numerOfJobs, $functionDefinitions)

    $jobs = foreach($i in 1..$numerOfJobs) {
        Start-ThreadJob -ScriptBlock {
            # bring the functions definition to this scope
            $helloFunc, $threadJobFunc = $using:functionDefinitions
            # define them in this scope
            ${function:Say-Hello} = $helloFunc
            ${function:From-ThreadJob} = $threadJobFunc
            # sleep random seconds
            Start-Sleep (Get-Random -Maximum 10)
            # combine the output from both functions
            (Say-Hello) + (From-ThreadJob -i $using:i)
        }
    }

    Receive-Job $jobs -AutoRemoveJob -Wait
}

Run-Jobs -numerOfJobs 10 -functionDefinitions $def

The method you can use to pass the function's definition to a different scope is the same for Invoke-Command (when PSRemoting), Start-Job, Start-ThreadJob and ForeEach-Object -Parallel. Since you want to invoke 2 different functions in your job's script block, I don't think -InitializationScript is an option, and even if it is, it might make the code even more complicated than it should be.

You can use this as an example of how you can store 2 function definitions in an array ($def), which is then passed to the scope of each TreadJob, this array is then used to define each function in said scope to be later used by each Job.

function Say-Hello {
    "Hello world!"
}

function From-ThreadJob {
    param($i)
    "From ThreadJob # $i"
}

$def = @(
    ${function:Say-Hello}.ToString()
    ${function:From-ThreadJob}.ToString()
)

function Run-Jobs {
    param($numerOfJobs, $functionDefinitions)

    $jobs = foreach($i in 1..$numerOfJobs) {
        Start-ThreadJob -ScriptBlock {
            # bring the functions definition to this scope
            $helloFunc, $threadJobFunc = $using:functionDefinitions
            # define them in this scope
            ${function:Say-Hello} = $helloFunc
            ${function:From-ThreadJob} = $threadJobFunc
            # sleep random seconds
            Start-Sleep (Get-Random -Maximum 10)
            # combine the output from both functions
            (Say-Hello) + (From-ThreadJob -i $using:i)
        }
    }

    Receive-Job $jobs -AutoRemoveJob -Wait
}

Run-Jobs -numerOfJobs 10 -functionDefinitions $def
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文