Class: LightIO::Core::Beam
- Inherits:
-
LightFiber
- Object
- Fiber
- LightFiber
- LightIO::Core::Beam
- Defined in:
- lib/lightio/core/beam.rb
Overview
Beam is light-weight executor, provide thread-like interface
@Example:
#- initialize with block
b = Beam.new{puts "hello"}
b.join
#output: hello
b = Beam.new(1,2,3){|one, two, three| puts [one, two, three].join(",") }
b.join
#output: 1,2,3
#- use join wait beam done
b = Beam.new(){LightIO.sleep 3}
b.join
b.alive? # false
Defined Under Namespace
Classes: BeamError
Constant Summary
Constants inherited from LightFiber
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#on_dead ⇒ Object
Returns the value of attribute on_dead.
Attributes inherited from LightFiber
Class Method Summary collapse
-
.pass ⇒ nil
Schedule beams.
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(*args, &blk) ⇒ Beam
constructor
Create a new beam.
-
#join(limit = nil) ⇒ Beam?
Block and wait beam dead.
-
#kill ⇒ Beam
Kill beam.
-
#raise(error, message = nil, backtrace = nil) ⇒ Object
Fiber not provide raise method, so we have to simulate one.
-
#value ⇒ Object
block and wait beam return a value.
Methods inherited from LightFiber
Constructor Details
#initialize(*args, &blk) ⇒ Beam
Create a new beam
Beam is light-weight executor, provide thread-like interface
Beam.new(“hello”){|hello| puts hello }
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lightio/core/beam.rb', line 47 def initialize(*args, &blk) raise Error, "must be called with a block" unless blk super() { begin @value = yield(*args) rescue StandardError => e @error = e end # mark as dead dead # transfer back to parent(caller fiber) after schedule parent.transfer } # schedule beam in ioloop ioloop.add_callback {transfer} @alive = true end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
35 36 37 |
# File 'lib/lightio/core/beam.rb', line 35 def error @error end |
#on_dead ⇒ Object
Returns the value of attribute on_dead.
36 37 38 |
# File 'lib/lightio/core/beam.rb', line 36 def on_dead @on_dead end |
Class Method Details
Instance Method Details
#alive? ⇒ Boolean
65 66 67 |
# File 'lib/lightio/core/beam.rb', line 65 def alive? super && @alive end |
#join(limit = nil) ⇒ Beam?
Block and wait beam dead
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lightio/core/beam.rb', line 83 def join(limit=nil) # try directly get result if !alive? || limit.nil? || limit <= 0 # call value to raise error value return self end # set a transfer back timer parent = Beam.current timer = LightIO::Watchers::Timer.new(limit) timer.set_callback {parent.transfer} ioloop.add_timer(timer) ioloop.transfer if alive? nil else check_and_raise_error self end end |
#kill ⇒ Beam
Kill beam
109 110 111 112 113 |
# File 'lib/lightio/core/beam.rb', line 109 def kill dead parent.transfer if self == Beam.current self end |
#raise(error, message = nil, backtrace = nil) ⇒ Object
Fiber not provide raise method, so we have to simulate one
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/lightio/core/beam.rb', line 117 def raise(error, =nil, backtrace=nil) unless error.is_a?(BeamError) ||= error.respond_to?(:message) ? error. : nil backtrace ||= error.respond_to?(:backtrace) ? error.backtrace : nil super(error, , backtrace) end self.parent = error.parent if error.parent if Beam.current == self raise(error.error, , backtrace) else @error ||= error end end |
#value ⇒ Object
block and wait beam return a value
70 71 72 73 74 75 76 77 |
# File 'lib/lightio/core/beam.rb', line 70 def value if alive? self.parent = Beam.current ioloop.transfer end check_and_raise_error @value end |