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