Class: ParticlePool::Task
- Inherits:
-
Object
- Object
- ParticlePool::Task
- Defined in:
- lib/particle_pool/task.rb
Defined Under Namespace
Modules: Status
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #_upd ⇒ Object
- #alive? ⇒ Boolean
- #await(s = 0.01) ⇒ Object
- #await_sync(s = 0.01) ⇒ Object
- #done? ⇒ Boolean
-
#initialize(&b) ⇒ Task
constructor
A new instance of Task.
- #resume ⇒ Object
- #start(*params) ⇒ Object
Constructor Details
#initialize(&b) ⇒ Task
11 12 13 14 15 |
# File 'lib/particle_pool/task.rb', line 11 def initialize(&b) @value = nil @status = Status::NOT_STARTED @b = b end |
Instance Attribute Details
#status ⇒ Object (readonly)
Returns the value of attribute status.
2 3 4 |
# File 'lib/particle_pool/task.rb', line 2 def status @status end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
2 3 4 |
# File 'lib/particle_pool/task.rb', line 2 def value @value end |
Instance Method Details
#_upd ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/particle_pool/task.rb', line 58 def _upd if alive? @status = Status::PAUSED else @status = Status::DEAD end end |
#alive? ⇒ Boolean
37 38 39 |
# File 'lib/particle_pool/task.rb', line 37 def alive? @f.alive? end |
#await(s = 0.01) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/particle_pool/task.rb', line 45 def await(s=0.01) until done? Fiber.yield sleep(s) end @value end |
#await_sync(s = 0.01) ⇒ Object
53 54 55 56 |
# File 'lib/particle_pool/task.rb', line 53 def await_sync(s=0.01) sleep(s) until done? @value end |
#done? ⇒ Boolean
41 42 43 |
# File 'lib/particle_pool/task.rb', line 41 def done? @status == Status::DEAD end |
#resume ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/particle_pool/task.rb', line 28 def resume if @status == Status::PAUSED @status = Status::ALIVE @value = @f.resume(@value) end _upd() @status end |
#start(*params) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/particle_pool/task.rb', line 17 def start(*params) raise 'cannot start already started task' if @status != Status::NOT_STARTED @f = Fiber.new do |*params| @status = Status::ALIVE @b.(*params) end @value = @f.resume(*params) _upd() @status end |