class Strand
Attributes
fiber[R]
The strand's underlying fiber.
Public Class Methods
current()
click to toggle source
Get the currently running strand. Primarily used to access “strand local” variables.
# File lib/strand.rb, line 18 def self.current @@strands[Fiber.current] end
list()
click to toggle source
Return an array of all Strands that are alive.
# File lib/strand.rb, line 13 def self.list @@strands.values end
new(&block)
click to toggle source
Create and run a strand.
# File lib/strand.rb, line 42 def initialize(&block) # Initialize our "fiber local" storage. @locals = {} # Condition variable for joining. @join_cond = ConditionVariable.new # Create our fiber. @fiber = Fiber.new{ fiber_body(&block) } # Add us to the list of living strands. @@strands[@fiber] = self # Finally start the strand. resume end
pass()
click to toggle source
Yield the strand, but have EM resume it on the next tick.
# File lib/strand.rb, line 35 def self.pass fiber = Fiber.current EM.next_tick{ fiber.resume } Fiber.yield end
sleep(seconds)
click to toggle source
EM/fiber safe sleep.
# File lib/strand.rb, line 23 def self.sleep(seconds) fiber = Fiber.current EM::Timer.new(seconds){ fiber.resume } Fiber.yield end
yield()
click to toggle source
Alias for Fiber.yield.
# File lib/strand.rb, line 30 def self.yield Fiber.yield end
Public Instance Methods
[](name)
click to toggle source
Access to “strand local” variables, akin to “thread local” variables.
Strand.new do ... Strand.current[:connection].send(data) ... end
# File lib/strand.rb, line 94 def [](name) @locals[name] end
[]=(name, value)
click to toggle source
Access to “strand local” variables, akin to “thread local” variables.
Strand.new do ... Strand.current[:connection] = SomeConnectionClass.new(host, port) ... end
# File lib/strand.rb, line 104 def []=(name, value) @locals[name] = value end
alive?()
click to toggle source
Like Thread#alive? or Fiber#alive?
# File lib/strand.rb, line 77 def alive? @fiber.alive? end
join()
click to toggle source
Like Thread#join.
s1 = Strand.new{ Strand.sleep(1) } s2 = Strand.new{ Strand.sleep(1) } s1.join s2.join
# File lib/strand.rb, line 65 def join @join_cond.wait if alive? raise @exception if @exception true end
resume()
click to toggle source
Like Fiber#resume.
# File lib/strand.rb, line 72 def resume @fiber.resume end
value()
click to toggle source
Like Thread#value. Implicitly calls join
.
strand = Strand.new{ 1+2 } strand.value # => 3
# File lib/strand.rb, line 84 def value join and @value end