An easy way to achieve parallelism is to split your function into chunks (for example, by several number intervals in which it will search primes) and then run each chunk with Start-Job:$jobs = @()# gather all jobs into an array $jobs += Start-Job -ScriptBlock {MyPrimesFunction1}$jobs += Start-Job -ScriptBlock {MyPrimesFunction2}$jobs += Start-Job -ScriptBlock {MyPrimesFunction3}$jobs += Start-Job -ScriptBlock {MyPrimesFunction4}# wait for all jobsWait-Job $jobs | Out-Null# get result arrays from jobs$results = $jobs | Receive-Job$primes = @()# merge results into single arrayforeach ($result in $results) { $primes += $result}请注意,您的函数必须以素数数组的形式返回结果.并且您必须重写您的函数 4 次,每次使用不同的数字间隔.Notice that your function must return a result as an array of primes. And you must rewrite your function 4 times, each using different number intervals.作业方法依赖于系统进程管理(因为每个作业启动单独的 powershell.exe).另一种方法是使用运行空间.您可以阅读关于它的几篇博文.Approach with jobs relies on system process management (cause each job starts separate powershell.exe). Another approach is to use Runspaces. You can read several posts about it. 这篇关于Powershell 多线程数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 06:38