Class: Eldritch::Task
- Inherits:
-
Object
- Object
- Eldritch::Task
- Defined in:
- lib/eldritch/task.rb
Overview
Runs a block in parallel and allows for interaction with said block
Instance Attribute Summary collapse
-
#thread ⇒ Thread
readonly
Underlying ruby thread.
-
#value ⇒ Object
The return value of the task.
Instance Method Summary collapse
-
#abort ⇒ Object
Forces the task to end.
-
#initialize {|Task| ... } ⇒ Task
constructor
Creates a new Task instance.
-
#interrupt ⇒ Object
Interrupts the task.
-
#start ⇒ Object
Starts the task.
-
#wait ⇒ Object
Waits for the task to complete.
Constructor Details
Instance Attribute Details
#thread ⇒ Thread (readonly)
Returns underlying ruby thread.
9 10 11 |
# File 'lib/eldritch/task.rb', line 9 def thread @thread end |
#value ⇒ Object
The return value of the task
If the task is still running, it will block until it is done and then fetch the return value.
43 44 45 46 |
# File 'lib/eldritch/task.rb', line 43 def value wait @value end |
Instance Method Details
#abort ⇒ Object
Forces the task to end
This kills the underlying thread. This is a dangerous call.
51 52 53 |
# File 'lib/eldritch/task.rb', line 51 def abort @thread.kill end |
#interrupt ⇒ Object
Interrupts the task
This is done by raising an InterruptedError in the task block. This can be caught to perform cleanup before exiting. Tasks started with DSL#async will automatically handle the exception and stop cleanly. You can still handle the exception yourself.
61 62 63 |
# File 'lib/eldritch/task.rb', line 61 def interrupt @thread.raise InterruptedError.new end |
#start ⇒ Object
Starts the task
This will yield the task to the block passed in the constructor.
task = Eldritch::Task.new do |task|
# do something
end
task.start # calls the block in parallel
28 29 30 31 |
# File 'lib/eldritch/task.rb', line 28 def start @thread = Thread.new self, &@block @thread.task = self end |
#wait ⇒ Object
Waits for the task to complete
34 35 36 |
# File 'lib/eldritch/task.rb', line 34 def wait @thread.join end |