I need to run a function more than 200 times simultaneously with PowerShell. So far I have two options Start-Job and Start-ThreadJob. In both cases I use a "launcher" as is
$MyFunction = [scriptblock]::Create(@"
Function FunctionName {$function:FunctionName}
"@)
foreach($i in $loop){
Start-ThreadJob -name $JobName -ThrottleLimit 30 -InitializationScript $MyFunction -ScriptBlock {FunctionName $Using:var1 $Using:var2}
#Start-Job -name $JobName -InitializationScript $MyFunction -ScriptBlock {FunctionName $Using:var1 $Using:var2}
}
With Start-Job, it takes about 30 seconds for a job to start. I noticed two things
- This time depends on the function (here FunctionName). If there are more variables it takes more time. My function has about 15 variables. I create a PSCustomObject and some data are stored in a MySQL DB every 2 seconds.
- If I comment all my code, the time remains the same! But if I remove all the code the job is created instantly.
With Start-Job, it then takes about 30 * 200 = 6000 seconds which is really too long.
Now with Start-ThreadJob, every jobs start instantly. There is a but, otherwise I would not post here, the data in MySQL are stored every 10 seconds! which is really too long. I don't believe this is a MySQL issue but a performance issue with PowerShell.
Do you have more option to propose or do you know how to improve performance?
Thank you for your time. Yann