Class: Tpool::Block
- Inherits:
-
Object
- Object
- Tpool::Block
- Defined in:
- lib/tpool_block.rb
Instance Attribute Summary collapse
-
#res ⇒ Object
Returns the value of attribute res.
Instance Method Summary collapse
-
#done? ⇒ Boolean
Returns true if the asynced job is done running.
-
#error! ⇒ Object
Raises error if one has happened in the asynced job.
-
#initialize(args) ⇒ Block
constructor
Constructor.
-
#join ⇒ Object
Sleeps until the asynced job is done.
-
#kill ⇒ Object
Kills the current running job.
- #result(args = nil) ⇒ Object
-
#run ⇒ Object
Starts running whatever block it is holding.
-
#running? ⇒ Boolean
Returns true if the asynced job is still running.
-
#waiting? ⇒ Boolean
Returns true if the asynced job is still waiting to run.
Constructor Details
#initialize(args) ⇒ Block
Constructor. Should not be called manually.
5 6 7 8 9 10 11 12 |
# File 'lib/tpool_block.rb', line 5 def initialize(args) @args = args @tpool = @args[:tpool] @running = false @done = false @error = nil end |
Instance Attribute Details
#res ⇒ Object
Returns the value of attribute res.
2 3 4 |
# File 'lib/tpool_block.rb', line 2 def res @res end |
Instance Method Details
#done? ⇒ Boolean
Returns true if the asynced job is done running.
41 42 43 |
# File 'lib/tpool_block.rb', line 41 def done? return @done end |
#error! ⇒ Object
Raises error if one has happened in the asynced job.
57 58 59 60 61 62 63 |
# File 'lib/tpool_block.rb', line 57 def error! #Wait for error to get set if any. self.join #Raise it if it got set. raise @error if @error end |
#join ⇒ Object
Sleeps until the asynced job is done. If an error occurred in the job, that error will be raised when calling the method.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tpool_block.rb', line 66 def join if !@done @args[:thread_starts] << Thread.current begin Thread.stop rescue Exception sleep 0.1 while !@done end end return self end |
#kill ⇒ Object
Kills the current running job.
81 82 83 84 |
# File 'lib/tpool_block.rb', line 81 def kill Thread.pass while !self.done? and !self.running? @thread_running.raise Exception, "Should kill itself." if !self.done? and self.running? end |
#result(args = nil) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/tpool_block.rb', line 86 def result(args = nil) self.join if args and args[:wait] raise "Not done yet." unless self.done? self.error! return @res end |
#run ⇒ Object
Starts running whatever block it is holding.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tpool_block.rb', line 15 def run @thread_running = Thread.current @thread_running.priority = @tpool.args[:priority] if @tpool.args.key?(:priority) @running = true begin @res = @args[:blk].call(*@args[:args], &@args[:blk]) rescue Exception => e @error = e @args[:tpool].on_error_call(:error => e, :block => self) ensure @running = false @done = true @thread_running = nil end if @args[:thread_starts] @args[:thread_starts].each do |thread| thread.wakeup if thread.alive? end end return self end |
#running? ⇒ Boolean
Returns true if the asynced job is still running.
46 47 48 |
# File 'lib/tpool_block.rb', line 46 def running? return @running end |
#waiting? ⇒ Boolean
Returns true if the asynced job is still waiting to run.
51 52 53 54 |
# File 'lib/tpool_block.rb', line 51 def waiting? return true if !@done and !@running and !@error return false end |