小编Nic*_*ugh的帖子

我如何才能创建在完成时产生结果的工作?

问题

考虑你有 4 台机器。

  • 机器A很慢
  • 机器B是中速,
  • 机器C很快。
  • LocalHost 是超快的。

在每台远程机器上,您想对前 100 万个素数求和。您可以从本地主机执行此操作:

$servers = @("MachineA","MachineB","MachineC")
Invoke-Command -ComputerName $servers -ScriptBlock {
    Sum-FirstMillionPrimes
}
Run Code Online (Sandbox Code Playgroud)

正如所写的那样,在最慢的机器完成之前,不会显示(产生)结果。

为了加快速度,您尝试将其作为一项工作来执行:

$servers = @("MachineA","MachineB","MachineC")
Invoke-Command -ComputerName $servers -ScriptBlock {
    Sum-FirstMillionPrimes
} -AsJob

while ($null -ne (Get-Job)) {
    $doneChildJob = Get-Job | Wait-Job -Any
    $processResult = $doneChildJob | Receive-Job -AutoRemoveJob -Wait
    $processResult
}
Run Code Online (Sandbox Code Playgroud)

这仍然有同样的问题,因为根据文档(示例 8):

The command uses the AsJob parameter to run the command as a background
job. This command returns a job object that …
Run Code Online (Sandbox Code Playgroud)

powershell multithreading

5
推荐指数
1
解决办法
82
查看次数

标签 统计

multithreading ×1

powershell ×1