Sha256: 99de7c7f5766bc08013ef8aab70cab08e4ed3a6a8a996becdd8f35e7527ffc8e

Contents?: true

Size: 1.21 KB

Versions: 104

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'

# Let's see how a long-running blocking operation can be interrupted. Polyphony
# provides several APIs for interrupting an ongoing operation, and distinguishes
# between two different types of interruptions: *cancel* and *move on*. A
# *cancel* will interrupt an ongoing operation and raise an exception. A *move
# on* will interrupt an ongoing operation without raising an exception,
# optionally returning an arbitrary value as the result of that operation.

def nap(tag, t)
  puts "#{Time.now} #{tag} napping for #{t} seconds..."
  sleep t
ensure
  puts "#{Time.now} #{tag} done napping"
end

# The Kernel#cancel_after interrupts a blocking operation by raising a
# Polyphony::Cancel exception after the given timeout. If not rescued, the
# exception is propagated up the fiber hierarchy
spin do
  # cancel after 1 second
  cancel_after(1) { nap(:cancel, 2) }
rescue Polyphony::Cancel => e
  puts "got exception: #{e}"
end

# The Kernel#move_on_after interrupts a blocking operation by raising a
# Polyphony::MoveOn exception, which is silently swallowed by the fiber
spin do
  # move on after 1 second
  move_on_after(1) do
    nap(:move_on, 2)
  end
end

suspend

Version data entries

104 entries across 104 versions & 1 rubygems

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