Class: Async::Task
Instance Attribute Summary collapse
-
#ios ⇒ Object
readonly
Returns the value of attribute ios.
-
#reactor ⇒ Object
readonly
Returns the value of attribute reactor.
Class Method Summary collapse
Instance Method Summary collapse
- #bind(io) ⇒ Object
-
#initialize(ios, reactor, &block) ⇒ Task
constructor
A new instance of Task.
- #register(io, interests) ⇒ Object
- #run ⇒ Object
- #stop! ⇒ Object
- #with(io) ⇒ Object
Constructor Details
#initialize(ios, reactor, &block) ⇒ Task
Returns a new instance of Task.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/async/task.rb', line 31 def initialize(ios, reactor, &block) @ios = Hash[ ios.collect{|io| [io.fileno, reactor.wrap(io, self)]} ] @reactor = reactor @fiber = Fiber.new do set! begin yield(*@ios.values, self) # Async.logger.debug("Task #{self} completed normally.") rescue Interrupt # Async.logger.debug("Task #{self} interrupted: #{$!}") ensure close end end end |
Instance Attribute Details
#ios ⇒ Object (readonly)
Returns the value of attribute ios.
67 68 69 |
# File 'lib/async/task.rb', line 67 def ios @ios end |
#reactor ⇒ Object (readonly)
Returns the value of attribute reactor.
68 69 70 |
# File 'lib/async/task.rb', line 68 def reactor @reactor end |
Class Method Details
.current ⇒ Object
87 88 89 |
# File 'lib/async/task.rb', line 87 def self.current Thread.current[:async_task] or raise RuntimeError, "No async task available!" end |
Instance Method Details
#bind(io) ⇒ Object
79 80 81 |
# File 'lib/async/task.rb', line 79 def bind(io) @ios[io.fileno] ||= reactor.wrap(io, self) end |
#register(io, interests) ⇒ Object
83 84 85 |
# File 'lib/async/task.rb', line 83 def register(io, interests) @reactor.register(io, interests) end |
#run ⇒ Object
54 55 56 57 58 |
# File 'lib/async/task.rb', line 54 def run @fiber.resume return @fiber end |
#stop! ⇒ Object
60 61 62 63 64 65 |
# File 'lib/async/task.rb', line 60 def stop! if @fiber.alive? exception = Interrupt.new("Stop right now!") @fiber.resume(exception) end end |
#with(io) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/async/task.rb', line 70 def with(io) wrapper = @reactor.wrap(io, self) yield wrapper ensure wrapper.close io.close end |