Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Last active December 19, 2015 10:29

Revisions

  1. ♠ ace hacker revised this gist Jul 6, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions forking-worker-example.rb
    Original file line number Diff line number Diff line change
    @@ -17,9 +17,10 @@ def work(i)
    # If the limiter is full, then we will block until space permits.
    @limiter.enq(1)
    Thread.new do
    Process.wait fork {work(i)}
    begin Process.wait fork {work(i)}
    # Once we are done with our work and our process has exited,
    # we can allow another process to run.
    @limiter.deq
    ensure @limiter.deq
    end
    end
    end
  2. ♠ ace hacker revised this gist Jul 6, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions forking-worker-example.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@
    # Run at most 4 UNIX processes for any given time.
    @limiter = SizedQueue.new(4)

    # Trivial. Could be anything.
    def work(i)
    1 * i
    end
  3. ♠ ace hacker revised this gist Jul 6, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions forking-worker-example.rb
    Original file line number Diff line number Diff line change
    @@ -5,16 +5,20 @@

    require 'thread'

    # Run at most 4 UNIX processes for any given time.
    @limiter = SizedQueue.new(4)

    def work(i)
    1 * i
    end

    10_000.times do |i|
    # If the limiter is full, then we will block until space permits.
    @limiter.enq(1)
    Thread.new do
    Process.wait fork {work(i)}
    # Once we are done with our work and our process has exited,
    # we can allow another process to run.
    @limiter.deq
    end
    end
  4. ♠ ace hacker created this gist Jul 6, 2013.
    20 changes: 20 additions & 0 deletions forking-worker-example.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # A program that works jobs in newly created UNIX
    # process by calling fork. The number of processes
    # running at any given time is bounded by our use
    # of a sized queue.

    require 'thread'

    @limiter = SizedQueue.new(4)

    def work(i)
    1 * i
    end

    10_000.times do |i|
    @limiter.enq(1)
    Thread.new do
    Process.wait fork {work(i)}
    @limiter.deq
    end
    end