Class: Knj::Threadpool::Asynced

Inherits:
Object
  • Object
show all
Defined in:
lib/knj/threadpool.rb

Overview

An object of this class will be returned when calling ‘run_async’.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Asynced

Constructor. Should not be called manually.



219
220
221
# File 'lib/knj/threadpool.rb', line 219

def initialize(args)
  @args = args
end

Instance Method Details

#done?Boolean

Returns true if the asynced job is done running.

Returns:

  • (Boolean)


230
231
232
233
# File 'lib/knj/threadpool.rb', line 230

def done?
  return true if @args[:runned] or @args.empty? or @args[:error]
  return false
end

#error!Object

Raises error if one has happened in the asynced job.



242
243
244
# File 'lib/knj/threadpool.rb', line 242

def error!
  raise @args[:error] if @args.key?(:error)
end

#joinObject

Sleeps until the asynced job is done. If an error occurred in the job, that error will be raised when calling the method.



247
248
249
250
251
252
253
254
255
# File 'lib/knj/threadpool.rb', line 247

def join
  loop do
    self.error!
    break if self.done?
    sleep 0.1
  end
  
  self.error!
end

#result(args = nil) ⇒ Object

Returns the result of the job. If an error occurred in the job, that error will be raised when calling the method.



258
259
260
261
262
263
# File 'lib/knj/threadpool.rb', line 258

def result(args = nil)
  self.join if args and args[:wait]
  raise "Not done yet." unless self.done?
  self.error!
  return @args[:result]
end

#running?Boolean

Returns true if the asynced job is still running.

Returns:

  • (Boolean)


224
225
226
227
# File 'lib/knj/threadpool.rb', line 224

def running?
  return true if @args[:running]
  return false
end

#waiting?Boolean

Returns true if the asynced job is still waiting to run.

Returns:

  • (Boolean)


236
237
238
239
# File 'lib/knj/threadpool.rb', line 236

def waiting?
  return true if !@args.empty? and !@args[:running] and !@args[:runned]
  return false
end