Sha256: 6056917bd9128a09b88d771de57aa509ec8663647401c58ec275013db90e4bab

Contents?: true

Size: 837 Bytes

Versions: 47

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'

# Kernel#spin starts a new fiber
@controller = spin do
  @worker = spin do
    loop do
      # Each fiber has a mailbox for receiving messages
      peer, op, x, y = receive
      result = x.send(op, y)
      # The result is sent back to the "client"
      peer << result
    end
  end
  # The controller fiber will block until the worker is done (but notice that
  # the worker runs an infinite loop.)
  @worker.supervise(@worker, restart: :always)
end

def calc(op, x, y)
  # Send the job to the worker fiber...
  @worker << [Fiber.current, op, x, y]
  # ... and wait for the result
  receive
end

# wait for worker to start
snooze until @worker

p calc(:+, 2, 3)
p calc(:**, 2, 3)
p calc(:+, 2, nil)

# wait for the controller to terminate
@controller.await

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
polyphony-1.6 examples/core/calc_with_supervise.rb
polyphony-1.5 examples/core/calc_with_supervise.rb
polyphony-1.4 examples/core/calc_with_supervise.rb
polyphony-1.3 examples/core/calc_with_supervise.rb
polyphony-1.2.1 examples/core/calc_with_supervise.rb
polyphony-1.2 examples/core/calc_with_supervise.rb
polyphony-1.1.1 examples/core/calc_with_supervise.rb
polyphony-1.1 examples/core/calc_with_supervise.rb
polyphony-1.0.2 examples/core/calc_with_supervise.rb
polyphony-1.0.1 examples/core/calc_with_supervise.rb
polyphony-1.0 examples/core/calc_with_supervise.rb
polyphony-0.99.6 examples/core/calc_with_supervise.rb
polyphony-0.99.5 examples/core/calc_with_supervise.rb
polyphony-0.99.4 examples/core/calc_with_supervise.rb
polyphony-0.99.3 examples/core/calc_with_supervise.rb
polyphony-0.99.2 examples/core/calc_with_supervise.rb
polyphony-0.99.1 examples/core/calc_with_supervise.rb
polyphony-0.99 examples/core/calc_with_supervise.rb
polyphony-0.98 examples/core/calc_with_supervise.rb
polyphony-0.97 examples/core/calc_with_supervise.rb