Class: Async::Task

Inherits:
Node
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/async/task.rb

Overview

A task represents the state associated with the execution of an asynchronous block.

Instance Attribute Summary collapse

Attributes inherited from Node

#annotation, #children, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#annotate, #consume, #description, #inspect, #print_hierarchy, #reap, #traverse

Constructor Details

#initialize(reactor, parent = Task.current?, logger: nil, &block) ⇒ Task

Create a new task.

Parameters:

  • reactor (Async::Reactor)

    the reactor this task will run within.

  • parent (Async::Task) (defaults to: Task.current?)

    the parent task.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/async/task.rb', line 75

def initialize(reactor, parent = Task.current?, logger: nil, &block)
	super(parent || reactor)
	
	@reactor = reactor
	
	@status = :initialized
	@result = nil
	@finished = nil
	
	@logger = logger
	
	@fiber = make_fiber(&block)
end

Instance Attribute Details

#fiberObject (readonly)



107
108
109
# File 'lib/async/task.rb', line 107

def fiber
  @fiber
end

#reactorObject (readonly)



98
99
100
# File 'lib/async/task.rb', line 98

def reactor
  @reactor
end

#statusObject (readonly)



114
115
116
# File 'lib/async/task.rb', line 114

def status
  @status
end

Class Method Details

.currentAsync::Task

Lookup the Async::Task for the current fiber. Raise RuntimeError if none is available.

Returns:

Raises:

  • (RuntimeError)

    if task was not #set! for the current fiber.



184
185
186
# File 'lib/async/task.rb', line 184

def self.current
	Thread.current[:async_task] or raise RuntimeError, "No async task available!"
end

.current?Async::Task?

Check if there is a task defined for the current fiber.

Returns:



190
191
192
# File 'lib/async/task.rb', line 190

def self.current?
	Thread.current[:async_task]
end

.yield {|result| ... } ⇒ Object

Yield the unerlying result for the task. If the result is an Exception, then that result will be raised an its exception.

Yields:

  • (result)

    result of the task if a block if given.

Returns:

  • (Object)

    result of the task

Raises:

  • (Exception)

    if the result is an exception



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/async/task.rb', line 58

def self.yield
	if block_given?
		result = yield
	else
		result = Fiber.yield
	end
	
	if result.is_a? Exception
		raise result
	else
		return result
	end
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/async/task.rb', line 109

def alive?
	@fiber&.alive?
end

#async(*arguments, **options, &block) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/async/task.rb', line 127

def async(*arguments, **options, &block)
	task = Task.new(@reactor, self, **options, &block)
	
	task.run(*arguments)
	
	return task
end

#complete?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/async/task.rb', line 222

def complete?
	@status == :complete
end

#current?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/async/task.rb', line 194

def current?
	self.equal?(Thread.current[:async_task])
end

#failed?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/async/task.rb', line 210

def failed?
	@status == :failed
end

#finished?Boolean

Whether we can remove this node from the reactor graph.

Returns:

  • (Boolean)


206
207
208
# File 'lib/async/task.rb', line 206

def finished?
	super && @status != :running
end

#loggerObject



93
94
95
# File 'lib/async/task.rb', line 93

def logger
	@logger ||= @parent&.logger
end

#run(*arguments) ⇒ Object

Begin the execution of the task.



117
118
119
120
121
122
123
124
125
# File 'lib/async/task.rb', line 117

def run(*arguments)
	if @status == :initialized
		@status = :running
		
		@fiber.resume(*arguments)
	else
		raise RuntimeError, "Task already running!"
	end
end

#running?Boolean

Check if the task is running.

Returns:

  • (Boolean)


200
201
202
# File 'lib/async/task.rb', line 200

def running?
	@status == :running
end

#stop(later = false) ⇒ void

This method returns an undefined value.

Stop the task and all of its children.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/async/task.rb', line 155

def stop(later = false)
	if self.stopped?
		# If we already stopped this task... don't try to stop it again:
		return
	end
	
	if self.running?
		if self.current?
			if later
				@reactor << Stop::Later.new(self)
			else
				raise Stop, "Stopping current task!"
			end
		elsif @fiber&.alive?
			begin
				@fiber.resume(Stop.new)
			rescue FiberError
				@reactor << Stop::Later.new(self)
			end
		end
	else
		# We are not running, but children might be, so transition directly into stopped state:
		stop!
	end
end

#stopped?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/async/task.rb', line 218

def stopped?
	@status == :stopped
end

#stopping?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/async/task.rb', line 214

def stopping?
	@status == :stopping
end

#to_sObject



89
90
91
# File 'lib/async/task.rb', line 89

def to_s
	"\#<#{self.description} (#{@status})>"
end

#waitObject Also known as: result

Retrieve the current result of the task. Will cause the caller to wait until result is available.

Returns:

  • (Object)

    the final expression/result of the task's block.

Raises:

  • (RuntimeError)

    if the task's fiber is the current fiber.



138
139
140
141
142
143
144
145
146
147
# File 'lib/async/task.rb', line 138

def wait
	raise RuntimeError, "Cannot wait on own fiber" if Fiber.current.equal?(@fiber)
	
	if running?
		@finished ||= Condition.new
		@finished.wait
	else
		Task.yield{@result}
	end
end

#yieldObject

Yield back to the reactor and allow other fibers to execute.



102
103
104
# File 'lib/async/task.rb', line 102

def yield
	Task.yield{reactor.yield}
end