none
How To Use Semaphores? RRS feed

  • Question

  • Hello Community,

    I have a question how to use semaphores to control the maximum count of parallel jobs in PowerShell.

    What I want to do is, that maximal three jobs should run parallel and the other jobs waits until one of the three is ready. Here my prototyping code:

    #-Begin-----------------------------------------------------------------
    
      Try {
        $oSemaphore = [System.Threading.Semaphore]::OpenExisting("Test");
      } Catch {
        $oSemaphore = [System.Threading.Semaphore]::New(3, 3, "Test");
      }
    
      For($i = 1; $i -le 5; $i++) {
    
        $Job = Start-Job -ArgumentList $oSemaphore, $i -ScriptBlock {
    
          Param($oSemaphore, $i);
    
          $ps = [PowerShell]::Create();
          If($i -le 3) {
            [Void] $ps.AddScript('Start-Sleep -Seconds 10;[System.Diagnostics.Debug]::WriteLine("Hello");');
          } Else {
            [Void] $ps.AddScript('[System.Diagnostics.Debug]::WriteLine("Hello2");');
          }
    
          $oSemaphore.WaitOne();
          [System.Diagnostics.Debug]::WriteLine("Job $i starts now");
          $ps.Invoke();
          [System.Diagnostics.Debug]::WriteLine("Job $i ends now");
          $oSemaphore.Release();
    
        }
    
      }
    
      Wait-Job * > $Null;
    
      Get-Job | Remove-Job
    
      $oSemaphore.Close();
    
    #-End-------------------------------------------------------------------
    

    Here the result:

    It works not really, as you can see. What I expected is like this:

    Job 1 starts now
    Job 2 starts now
    Job 3 starts now
    Job 4 starts now
    Job 5 start now
    Job 1 ends now
    Job 4 ends now
    Job 2 ends now
    Job 5 ends now
    Job 3 ends now

    Is it generally possible to control the maximum count of jobs via semaphores?

    Thanks for tips and hints.

    Cheers
    Stefan

    Thursday, November 2, 2017 6:17 AM

Answers

  • Almost anything you can think of doing is already designed into PowerShell.  PS is an aggressive exposure of Net functionality wrapped up in an easier to use syntax and with most of the detailed work done internally.

    To effectively use PS requires an in depth study of the capabilities.  A good book that gives a great overview and as much detail as you choose to dig into is the newest PS book by Bruce Payette and Richard Siddaway Payette is one of the designers of PS.

    PowerShell in  Action 3rd Edition


    \_(ツ)_/

    Friday, November 3, 2017 6:19 AM
  • This can best be managed using a workflow.  Workflows can be declared to limit the number of threads used.  You can set the maximum concurrency and control most other elements of a multi-threaded collection.


    \_(ツ)_/

    Thursday, November 2, 2017 4:12 PM

All replies

  • This can best be managed using a workflow.  Workflows can be declared to limit the number of threads used.  You can set the maximum concurrency and control most other elements of a multi-threaded collection.


    \_(ツ)_/

    Thursday, November 2, 2017 4:12 PM
  • Hello jrv,

    thank you very much for this interesting hint.

    Best regards
    Stefan

    Friday, November 3, 2017 6:12 AM
  • Almost anything you can think of doing is already designed into PowerShell.  PS is an aggressive exposure of Net functionality wrapped up in an easier to use syntax and with most of the detailed work done internally.

    To effectively use PS requires an in depth study of the capabilities.  A good book that gives a great overview and as much detail as you choose to dig into is the newest PS book by Bruce Payette and Richard Siddaway Payette is one of the designers of PS.

    PowerShell in  Action 3rd Edition


    \_(ツ)_/

    Friday, November 3, 2017 6:19 AM
  • Hello jrv,

    thanks again for this clarification and the book tip, it looks very good.

    Best regards
    Stefan

    Friday, November 3, 2017 7:06 AM