Skip to content

Instantly share code, notes, and snippets.

@knjname
Created January 14, 2015 16:11
Show Gist options
  • Save knjname/07170e37016675284595 to your computer and use it in GitHub Desktop.
Save knjname/07170e37016675284595 to your computer and use it in GitHub Desktop.
An implementation of constant sized concurrent job queue in PowerShell by Start-Job cmdlet.
$jobQueueLength = 4
$todoList = "Ada", "BASIC", "C++", "D", "Eiffel", "F#", "Groovy", "Haskell", "IO", "Java", "K", "Lua", "VBA"
$job = {
param($languageName)
echo "I like ..."
Sleep -Milliseconds (Random 2000)
echo " ${languageName}!"
}
$emptyJob = Start-Job -ScriptBlock {}
$jobQueue = 1..$jobQueueLength | % { $emptyJob }
$loopInterval = 100
$continuing = $true
for (; $continuing ; Sleep -Milliseconds $loopInterval ) {
for ($i = 0; $i -lt $jobQueue.Length; $i++) {
$queuedJob = $jobQueue[$i]
if ($queuedJob.State -eq "Completed" -or
$queuedJob.State -eq "Failed") {
$jobArg, $todoList = $todoList
if($jobArg -ne $null) {
$jobQueue[$i] = Start-Job -ScriptBlock $job -ArgumentList $jobArg
} else {
Wait-Job -Job $jobQueue
$continuing = $false
}
}
}
Receive-Job -Job $jobQueue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment