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.
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.
-
#join ⇒ Object
Waits for the task to complete.
-
#start ⇒ Object
Starts the task.
-
#value ⇒ Object
The return value of the task.
Constructor Details
Instance Attribute Details
#thread ⇒ Thread (readonly)
Returns underlying ruby thread.
5 6 7 |
# File 'lib/eldritch/task.rb', line 5 def thread @thread end |
Instance Method Details
#abort ⇒ Object
Forces the task to end
This kills the underlying thread. This is a dangerous call.
49 50 51 52 |
# File 'lib/eldritch/task.rb', line 49 def abort @thread.kill unset_thread_task 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.
60 61 62 63 |
# File 'lib/eldritch/task.rb', line 60 def interrupt @thread.raise InterruptedError.new unset_thread_task end |
#join ⇒ Object
Waits for the task to complete
30 31 32 33 |
# File 'lib/eldritch/task.rb', line 30 def join @thread.join unset_thread_task end |
#start ⇒ Object
24 25 26 27 |
# File 'lib/eldritch/task.rb', line 24 def start @thread = Thread.new &@block @thread.eldritch_task = self 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.
40 41 42 43 44 |
# File 'lib/eldritch/task.rb', line 40 def value val = @thread.value unset_thread_task val end |