Sha256: 90d90833e10c62c250e9403660b7eec7e96802ef9867a6c0c15e35b935819e7a

Contents?: true

Size: 895 Bytes

Versions: 47

Compression:

Stored size: 895 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.await
rescue => e
  puts "Uncaught exception in worker: #{e}. Restarting..."
  @worker.restart
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_restart.rb
polyphony-1.5 examples/core/calc_with_restart.rb
polyphony-1.4 examples/core/calc_with_restart.rb
polyphony-1.3 examples/core/calc_with_restart.rb
polyphony-1.2.1 examples/core/calc_with_restart.rb
polyphony-1.2 examples/core/calc_with_restart.rb
polyphony-1.1.1 examples/core/calc_with_restart.rb
polyphony-1.1 examples/core/calc_with_restart.rb
polyphony-1.0.2 examples/core/calc_with_restart.rb
polyphony-1.0.1 examples/core/calc_with_restart.rb
polyphony-1.0 examples/core/calc_with_restart.rb
polyphony-0.99.6 examples/core/calc_with_restart.rb
polyphony-0.99.5 examples/core/calc_with_restart.rb
polyphony-0.99.4 examples/core/calc_with_restart.rb
polyphony-0.99.3 examples/core/calc_with_restart.rb
polyphony-0.99.2 examples/core/calc_with_restart.rb
polyphony-0.99.1 examples/core/calc_with_restart.rb
polyphony-0.99 examples/core/calc_with_restart.rb
polyphony-0.98 examples/core/calc_with_restart.rb
polyphony-0.97 examples/core/calc_with_restart.rb