You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: context/scheduler.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ Async do |task|
38
38
task.print_hierarchy($stderr)
39
39
40
40
# Kill the subtask
41
-
subtask.stop
41
+
subtask.cancel
42
42
end
43
43
~~~
44
44
@@ -69,9 +69,9 @@ end
69
69
70
70
You can use this approach to embed the reactor in another event loop. For some integrations, you may want to specify the maximum time to wait to {ruby Async::Scheduler#run_once}.
71
71
72
-
### Stopping a Scheduler
72
+
### Cancelling a Scheduler
73
73
74
-
{ruby Async::Scheduler#stop} will stop the current scheduler and all children tasks.
74
+
{ruby Async::Scheduler#cancel} will cancel the current scheduler and all children tasks.
Copy file name to clipboardExpand all lines: context/tasks.md
+28-28Lines changed: 28 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ This guide explains how asynchronous tasks work and how to use them.
4
4
5
5
## Overview
6
6
7
-
Tasks are the smallest unit of sequential code execution in {ruby Async}. Tasks can create other tasks, and Async tracks the parent-child relationship between tasks. When a parent task is stopped, it will also stop all its children tasks. The reactor always starts with one root task.
7
+
Tasks are the smallest unit of sequential code execution in {ruby Async}. Tasks can create other tasks, and Async tracks the parent-child relationship between tasks. When a parent task is cancelled, it will also cancel all its children tasks. The reactor always starts with one root task.
8
8
9
9
```mermaid
10
10
graph LR
@@ -23,11 +23,11 @@ graph LR
23
23
24
24
A fiber is a lightweight unit of execution that can be suspended and resumed at specific points. After a fiber is suspended, it can be resumed later at the same point with the same execution state. Because only one fiber can execute at a time, they are often referred to as a mechanism for cooperative concurrency.
25
25
26
-
A task provides extra functionality on top of fibers. A task behaves like a promise: it either succeeds with a value or fails with an exception. Tasks keep track of their parent-child relationships, and when a parent task is stopped, it will also stop all its children tasks. This makes it easier to create complex programs with many concurrent tasks.
26
+
A task provides extra functionality on top of fibers. A task behaves like a promise: it either succeeds with a value or fails with an exception. Tasks keep track of their parent-child relationships, and when a parent task is cancelled, it will also cancel all its children tasks. This makes it easier to create complex programs with many concurrent tasks.
27
27
28
28
### Why does Async manipulate tasks and not fibers?
29
29
30
-
The {ruby Async::Scheduler} actually works directly with fibers for most operations and isn't aware of tasks. However, the reactor does maintain a tree of tasks for the purpose of managing task and reactor life-cycle. For example, stopping a parent task will stop all its children tasks, and the reactor will exit when all tasks are finished.
30
+
The {ruby Async::Scheduler} actually works directly with fibers for most operations and isn't aware of tasks. However, the reactor does maintain a tree of tasks for the purpose of managing task and reactor life-cycle. For example, cancelling a parent task will cancel all its children tasks, and the reactor will exit when all tasks are finished.
Tasks are created in the `initialized` state, and are run by the reactor. During the execution, a task can either `complete` successfully, become `failed` with an unhandled `StandardError`-derived exception, or be explicitly `stopped`. In all of these cases, you can wait for a task to complete by using {ruby Async::Task#wait}.
52
+
Tasks are created in the `initialized` state, and are run by the reactor. During the execution, a task can either `complete` successfully, become `failed` with an unhandled `StandardError`-derived exception, or be explicitly `cancelled`. In all of these cases, you can wait for a task to complete by using {ruby Async::Task#wait}.
53
53
54
54
1. In the case the task successfully completed, the result will be whatever value was generated by the last expression in the task.
55
55
2. In the case the task failed with an unhandled `StandardError`-derived exception, waiting on the task will re-raise the exception.
56
-
3. In the case the task was stopped, the result will be `nil`.
56
+
3. In the case the task was cancelled, the result will be `nil`.
57
57
58
58
## Starting A Task
59
59
@@ -175,8 +175,8 @@ Async do
175
175
breakif done.size >=2
176
176
end
177
177
ensure
178
-
# The remainder of the tasks will be stopped:
179
-
barrier.stop
178
+
# The remainder of the tasks will be cancelled:
179
+
barrier.cancel
180
180
end
181
181
end
182
182
```
@@ -199,18 +199,18 @@ begin
199
199
# Wait until all jobs are done:
200
200
barrier.wait
201
201
ensure
202
-
#Stop any remaining jobs:
203
-
barrier.stop
202
+
#Cancel any remaining jobs:
203
+
barrier.cancel
204
204
end
205
205
~~~
206
206
207
-
## Stopping a Task
207
+
## Cancelling a Task
208
208
209
209
When a task completes execution, it will enter the `complete` state (or the `failed` state if it raises an unhandled exception).
210
210
211
-
There are various situations where you may want to stop a task ({ruby Async::Task#stop}) before it completes. The most common case is shutting down a server. A more complex example is this: you may fan out multiple (10s, 100s) of requests, wait for a subset to complete (e.g. the first 5 or all those that complete within a given deadline), and then stop (terminate/cancel) the remaining operations.
211
+
There are various situations where you may want to cancel a task ({ruby Async::Task#cancel}) before it completes. The most common case is shutting down a server. A more complex example is this: you may fan out multiple (10s, 100s) of requests, wait for a subset to complete (e.g. the first 5 or all those that complete within a given deadline), and then cancel the remaining operations.
212
212
213
-
Using the above program as an example, let's stop all the tasks just after the first one completes.
213
+
Using the above program as an example, let's cancel all the tasks just after the first one completes.
214
214
215
215
```ruby
216
216
Asyncdo
@@ -221,14 +221,14 @@ Async do
221
221
end
222
222
end
223
223
224
-
#Stop all the above tasks:
225
-
tasks.each(&:stop)
224
+
#Cancel all the above tasks:
225
+
tasks.each(&:cancel)
226
226
end
227
227
```
228
228
229
-
### Stopping all Tasks held in a Barrier
229
+
### Cancelling all Tasks held in a Barrier
230
230
231
-
To stop (terminate/cancel) all the tasks held in a barrier:
231
+
To cancel all the tasks held in a barrier:
232
232
233
233
```ruby
234
234
barrier =Async::Barrier.new
@@ -241,11 +241,11 @@ Async do
241
241
end
242
242
end
243
243
244
-
barrier.stop
244
+
barrier.cancel
245
245
end
246
246
```
247
247
248
-
Unless your tasks all rescue and suppresses `StandardError`-derived exceptions, be sure to call ({ruby Async::Barrier#stop}) to stop the remaining tasks:
248
+
Unless your tasks all rescue and suppresses `StandardError`-derived exceptions, be sure to call ({ruby Async::Barrier#cancel}) to cancel the remaining tasks:
249
249
250
250
```ruby
251
251
barrier =Async::Barrier.new
@@ -261,7 +261,7 @@ Async do
261
261
begin
262
262
barrier.wait
263
263
ensure
264
-
barrier.stop
264
+
barrier.cancel
265
265
end
266
266
end
267
267
```
@@ -273,10 +273,10 @@ In order to ensure your resources are cleaned up correctly, make sure you wrap r
273
273
~~~ruby
274
274
Asyncdo
275
275
begin
276
-
socket = connect(remote_address) # May raise Async::Stop
276
+
socket = connect(remote_address) # May raise Async::Cancel
277
277
278
-
socket.write(...) # May raise Async::Stop
279
-
socket.read(...) # May raise Async::Stop
278
+
socket.write(...) # May raise Async::Cancel
279
+
socket.read(...) # May raise Async::Cancel
280
280
ensure
281
281
socket.close if socket
282
282
end
@@ -398,9 +398,9 @@ end
398
398
399
399
Transient tasks are similar to normal tasks, except for the following differences:
400
400
401
-
1. They are not considered by {ruby Async::Task#finished?}, so they will not keep the reactor alive. Instead, they are stopped (with a {ruby Async::Stop} exception) when all other (non-transient) tasks are finished.
401
+
1. They are not considered by {ruby Async::Task#finished?}, so they will not keep the reactor alive. Instead, they are cancelled (with a {ruby Async::Cancel} exception) when all other (non-transient) tasks are finished.
402
402
2. As soon as a parent task is finished, any transient child tasks will be moved up to be children of the parent's parent. This ensures that they never keep a sub-tree alive.
403
-
3. Similarly, if you `stop` a task, any transient child tasks will be moved up the tree as above rather than being stopped.
403
+
3. Similarly, if you `cancel` a task, any transient child tasks will be moved up the tree as above rather than being cancelled.
404
404
405
405
The purpose of transient tasks is when a task is an implementation detail of an object or instance, rather than a concurrency process. Some examples of transient tasks:
406
406
@@ -439,7 +439,7 @@ class TimeStringCache
439
439
sleep(1)
440
440
end
441
441
ensure
442
-
# When the reactor terminates all tasks, `Async::Stop` will be raised from `sleep` and this code will be invoked. By clearing `@refresh`, we ensure that the task will be recreated if needed again:
442
+
# When the reactor terminates all tasks, `Async::Cancel` will be raised from `sleep` and this code will be invoked. By clearing `@refresh`, we ensure that the task will be recreated if needed again:
Copy file name to clipboardExpand all lines: guides/scheduler/readme.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ Async do |task|
38
38
task.print_hierarchy($stderr)
39
39
40
40
# Kill the subtask
41
-
subtask.stop
41
+
subtask.cancel
42
42
end
43
43
~~~
44
44
@@ -69,9 +69,9 @@ end
69
69
70
70
You can use this approach to embed the reactor in another event loop. For some integrations, you may want to specify the maximum time to wait to {ruby Async::Scheduler#run_once}.
71
71
72
-
### Stopping a Scheduler
72
+
### Cancelling a Scheduler
73
73
74
-
{ruby Async::Scheduler#stop} will stop the current scheduler and all children tasks.
74
+
{ruby Async::Scheduler#cancel} will cancel the current scheduler and all children tasks.
Copy file name to clipboardExpand all lines: guides/tasks/readme.md
+28-28Lines changed: 28 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ This guide explains how asynchronous tasks work and how to use them.
4
4
5
5
## Overview
6
6
7
-
Tasks are the smallest unit of sequential code execution in {ruby Async}. Tasks can create other tasks, and Async tracks the parent-child relationship between tasks. When a parent task is stopped, it will also stop all its children tasks. The reactor always starts with one root task.
7
+
Tasks are the smallest unit of sequential code execution in {ruby Async}. Tasks can create other tasks, and Async tracks the parent-child relationship between tasks. When a parent task is cancelled, it will also cancel all its children tasks. The reactor always starts with one root task.
8
8
9
9
```mermaid
10
10
graph LR
@@ -23,11 +23,11 @@ graph LR
23
23
24
24
A fiber is a lightweight unit of execution that can be suspended and resumed at specific points. After a fiber is suspended, it can be resumed later at the same point with the same execution state. Because only one fiber can execute at a time, they are often referred to as a mechanism for cooperative concurrency.
25
25
26
-
A task provides extra functionality on top of fibers. A task behaves like a promise: it either succeeds with a value or fails with an exception. Tasks keep track of their parent-child relationships, and when a parent task is stopped, it will also stop all its children tasks. This makes it easier to create complex programs with many concurrent tasks.
26
+
A task provides extra functionality on top of fibers. A task behaves like a promise: it either succeeds with a value or fails with an exception. Tasks keep track of their parent-child relationships, and when a parent task is cancelled, it will also cancel all its children tasks. This makes it easier to create complex programs with many concurrent tasks.
27
27
28
28
### Why does Async manipulate tasks and not fibers?
29
29
30
-
The {ruby Async::Scheduler} actually works directly with fibers for most operations and isn't aware of tasks. However, the reactor does maintain a tree of tasks for the purpose of managing task and reactor life-cycle. For example, stopping a parent task will stop all its children tasks, and the reactor will exit when all tasks are finished.
30
+
The {ruby Async::Scheduler} actually works directly with fibers for most operations and isn't aware of tasks. However, the reactor does maintain a tree of tasks for the purpose of managing task and reactor life-cycle. For example, cancelling a parent task will cancel all its children tasks, and the reactor will exit when all tasks are finished.
Tasks are created in the `initialized` state, and are run by the reactor. During the execution, a task can either `complete` successfully, become `failed` with an unhandled `StandardError`-derived exception, or be explicitly `stopped`. In all of these cases, you can wait for a task to complete by using {ruby Async::Task#wait}.
52
+
Tasks are created in the `initialized` state, and are run by the reactor. During the execution, a task can either `complete` successfully, become `failed` with an unhandled `StandardError`-derived exception, or be explicitly `cancelled`. In all of these cases, you can wait for a task to complete by using {ruby Async::Task#wait}.
53
53
54
54
1. In the case the task successfully completed, the result will be whatever value was generated by the last expression in the task.
55
55
2. In the case the task failed with an unhandled `StandardError`-derived exception, waiting on the task will re-raise the exception.
56
-
3. In the case the task was stopped, the result will be `nil`.
56
+
3. In the case the task was cancelled, the result will be `nil`.
57
57
58
58
## Starting A Task
59
59
@@ -175,8 +175,8 @@ Async do
175
175
breakif done.size >=2
176
176
end
177
177
ensure
178
-
# The remainder of the tasks will be stopped:
179
-
barrier.stop
178
+
# The remainder of the tasks will be cancelled:
179
+
barrier.cancel
180
180
end
181
181
end
182
182
```
@@ -199,18 +199,18 @@ begin
199
199
# Wait until all jobs are done:
200
200
barrier.wait
201
201
ensure
202
-
#Stop any remaining jobs:
203
-
barrier.stop
202
+
#Cancel any remaining jobs:
203
+
barrier.cancel
204
204
end
205
205
~~~
206
206
207
-
## Stopping a Task
207
+
## Cancelling a Task
208
208
209
209
When a task completes execution, it will enter the `complete` state (or the `failed` state if it raises an unhandled exception).
210
210
211
-
There are various situations where you may want to stop a task ({ruby Async::Task#stop}) before it completes. The most common case is shutting down a server. A more complex example is this: you may fan out multiple (10s, 100s) of requests, wait for a subset to complete (e.g. the first 5 or all those that complete within a given deadline), and then stop (terminate/cancel) the remaining operations.
211
+
There are various situations where you may want to cancel a task ({ruby Async::Task#cancel}) before it completes. The most common case is shutting down a server. A more complex example is this: you may fan out multiple (10s, 100s) of requests, wait for a subset to complete (e.g. the first 5 or all those that complete within a given deadline), and then cancel the remaining operations.
212
212
213
-
Using the above program as an example, let's stop all the tasks just after the first one completes.
213
+
Using the above program as an example, let's cancel all the tasks just after the first one completes.
214
214
215
215
```ruby
216
216
Asyncdo
@@ -221,14 +221,14 @@ Async do
221
221
end
222
222
end
223
223
224
-
#Stop all the above tasks:
225
-
tasks.each(&:stop)
224
+
#Cancel all the above tasks:
225
+
tasks.each(&:cancel)
226
226
end
227
227
```
228
228
229
-
### Stopping all Tasks held in a Barrier
229
+
### Cancelling all Tasks held in a Barrier
230
230
231
-
To stop (terminate/cancel) all the tasks held in a barrier:
231
+
To cancel all the tasks held in a barrier:
232
232
233
233
```ruby
234
234
barrier =Async::Barrier.new
@@ -241,11 +241,11 @@ Async do
241
241
end
242
242
end
243
243
244
-
barrier.stop
244
+
barrier.cancel
245
245
end
246
246
```
247
247
248
-
Unless your tasks all rescue and suppresses `StandardError`-derived exceptions, be sure to call ({ruby Async::Barrier#stop}) to stop the remaining tasks:
248
+
Unless your tasks all rescue and suppresses `StandardError`-derived exceptions, be sure to call ({ruby Async::Barrier#cancel}) to cancel the remaining tasks:
249
249
250
250
```ruby
251
251
barrier =Async::Barrier.new
@@ -261,7 +261,7 @@ Async do
261
261
begin
262
262
barrier.wait
263
263
ensure
264
-
barrier.stop
264
+
barrier.cancel
265
265
end
266
266
end
267
267
```
@@ -273,10 +273,10 @@ In order to ensure your resources are cleaned up correctly, make sure you wrap r
273
273
~~~ruby
274
274
Asyncdo
275
275
begin
276
-
socket = connect(remote_address) # May raise Async::Stop
276
+
socket = connect(remote_address) # May raise Async::Cancel
277
277
278
-
socket.write(...) # May raise Async::Stop
279
-
socket.read(...) # May raise Async::Stop
278
+
socket.write(...) # May raise Async::Cancel
279
+
socket.read(...) # May raise Async::Cancel
280
280
ensure
281
281
socket.close if socket
282
282
end
@@ -398,9 +398,9 @@ end
398
398
399
399
Transient tasks are similar to normal tasks, except for the following differences:
400
400
401
-
1. They are not considered by {ruby Async::Task#finished?}, so they will not keep the reactor alive. Instead, they are stopped (with a {ruby Async::Stop} exception) when all other (non-transient) tasks are finished.
401
+
1. They are not considered by {ruby Async::Task#finished?}, so they will not keep the reactor alive. Instead, they are cancelled (with a {ruby Async::Cancel} exception) when all other (non-transient) tasks are finished.
402
402
2. As soon as a parent task is finished, any transient child tasks will be moved up to be children of the parent's parent. This ensures that they never keep a sub-tree alive.
403
-
3. Similarly, if you `stop` a task, any transient child tasks will be moved up the tree as above rather than being stopped.
403
+
3. Similarly, if you `cancel` a task, any transient child tasks will be moved up the tree as above rather than being cancelled.
404
404
405
405
The purpose of transient tasks is when a task is an implementation detail of an object or instance, rather than a concurrency process. Some examples of transient tasks:
406
406
@@ -439,7 +439,7 @@ class TimeStringCache
439
439
sleep(1)
440
440
end
441
441
ensure
442
-
# When the reactor terminates all tasks, `Async::Stop` will be raised from `sleep` and this code will be invoked. By clearing `@refresh`, we ensure that the task will be recreated if needed again:
442
+
# When the reactor terminates all tasks, `Async::Cancel` will be raised from `sleep` and this code will be invoked. By clearing `@refresh`, we ensure that the task will be recreated if needed again:
0 commit comments