async 2.35.2 → 2.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28fcbed588ccb0c02ebed08bfc2d316b07e34563cb52fc639f33d5146c7ca9bc
4
- data.tar.gz: b9319321c6acb3a6d48b0e76a2afbc3c017586855c6c842950eb0376db8b1bbb
3
+ metadata.gz: b514097c721290749f38fed73721c5cb6d17af2a8af189f5a8e537f7b30c14d3
4
+ data.tar.gz: 3eba30b0a03bdb9f9da1d1e4eeb6b11ec3224c0f3d02f27cc07ac1e5e7d7a41e
5
5
  SHA512:
6
- metadata.gz: 689e40bffc89b7bc39c1358ef9ab69b5bd49f5840790ebd8e3ebbcfe09d4edbefcd3813e7d4ab8165783a6613624cb1625921140ecb45c124dafc6b5dc424425
7
- data.tar.gz: 658be41e0d4d602f3a6167f5c380268eff5000114dd117d0edbe0ba8ddda25061935119f07d2f21e286892f46f33b779c785d9104de5a3f4cea10d661a42e093
6
+ metadata.gz: 9ea8d371279ec9bf8feeaa85895f25733247c8ecb059d599f34e8ecc85b4b124c05dee5421414d28c24316d7c4e69bdff622c31b182e9f883456836a6aab17ed
7
+ data.tar.gz: 55ddefa4f011c2dccf235043cad5ad334418c394ced59a960b993d18ead0850f7d60875a3edbdc55418d3a9d3045cef1d607170fea0ae484f4dc62bc13f15ca2
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/async/clock.rb CHANGED
@@ -36,6 +36,9 @@ module Async
36
36
  @started = nil
37
37
  end
38
38
 
39
+ # @returns [Numeric | Nil] The time when the clock was started, or nil if not started.
40
+ attr :started
41
+
39
42
  # Start measuring a duration.
40
43
  def start!
41
44
  @started ||= Clock.now
@@ -70,5 +73,22 @@ module Async
70
73
  @started = Clock.now
71
74
  end
72
75
  end
76
+
77
+ # Convert the clock to a JSON-compatible hash.
78
+ #
79
+ # @returns [Hash] The JSON-compatible hash.
80
+ def as_json(...)
81
+ {
82
+ started: self.started,
83
+ total: self.total,
84
+ }
85
+ end
86
+
87
+ # Convert the clock to a JSON string.
88
+ #
89
+ # @returns [String] The JSON string.
90
+ def to_json(...)
91
+ self.as_json.to_json(...)
92
+ end
73
93
  end
74
94
  end
data/lib/async/node.rb CHANGED
@@ -295,6 +295,12 @@ module Async
295
295
  end
296
296
  end
297
297
 
298
+ # Wait for this node to complete. By default, nodes cannot be waited on.
299
+ # Subclasses like Task override this method to provide waiting functionality.
300
+ def wait
301
+ nil
302
+ end
303
+
298
304
  # Whether the node has been stopped.
299
305
  def stopped?
300
306
  @children.nil?
data/lib/async/task.rb CHANGED
@@ -31,6 +31,32 @@ module Async
31
31
  end
32
32
  end
33
33
 
34
+ # Represents a sequential unit of work, defined by a block, which is executed concurrently with other tasks. A task can be in one of the following states: `initialized`, `running`, `completed`, `failed`, `cancelled` or `stopped`.
35
+ #
36
+ # ```mermaid
37
+ # stateDiagram-v2
38
+ # [*] --> Initialized
39
+ # Initialized --> Running : Run
40
+ #
41
+ # Running --> Completed : Return Value
42
+ # Running --> Failed : Exception
43
+ #
44
+ # Completed --> [*]
45
+ # Failed --> [*]
46
+ #
47
+ # Running --> Stopped : Stop
48
+ # Stopped --> [*]
49
+ # Completed --> Stopped : Stop
50
+ # Failed --> Stopped : Stop
51
+ # Initialized --> Stopped : Stop
52
+ # ```
53
+ #
54
+ # @example Creating a task that sleeps for 1 second.
55
+ # require "async"
56
+ # Async do |task|
57
+ # sleep(1)
58
+ # end
59
+ #
34
60
  # @public Since *Async v1*.
35
61
  class Task < Node
36
62
  # Raised when a child task is created within a task that has finished execution.
@@ -258,11 +284,43 @@ module Async
258
284
  begin
259
285
  @promise.wait
260
286
  rescue Promise::Cancel
261
- # For backward compatibility, stopped tasks return nil
287
+ # For backward compatibility, stopped tasks return nil:
262
288
  return nil
263
289
  end
264
290
  end
265
291
 
292
+ # For compatibility with `Thread#join` and similar interfaces.
293
+ alias join wait
294
+
295
+ # Wait on all non-transient children to complete, recursively, then wait on the task itself, if it is not the current task.
296
+ #
297
+ # If any child task fails with an exception, that exception will be raised immediately, and remaining children may not be waited on.
298
+ #
299
+ # @example Waiting on all children.
300
+ # Async do |task|
301
+ # child = task.async do
302
+ # sleep(0.01)
303
+ # end
304
+ # task.wait_all # Will wait on the child task.
305
+ # end
306
+ #
307
+ # @raises [StandardError] If any child task failed with an exception, that exception will be raised.
308
+ # @returns [Object | Nil] The final expression/result of the task's block, or nil if called from within the task.
309
+ # @asynchronous This method is thread-safe.
310
+ def wait_all
311
+ @children&.each do |child|
312
+ # Skip transient tasks
313
+ next if child.transient?
314
+
315
+ child.wait_all
316
+ end
317
+
318
+ # Only wait on the task if we're not waiting on ourselves:
319
+ unless self.current?
320
+ return self.wait
321
+ end
322
+ end
323
+
266
324
  # Access the result of the task without waiting. May be nil if the task is not completed. Does not raise exceptions.
267
325
  def result
268
326
  value = @promise.value
data/lib/async/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2017-2025, by Samuel Williams.
5
5
 
6
6
  module Async
7
- VERSION = "2.35.2"
7
+ VERSION = "2.36.0"
8
8
  end
data/readme.md CHANGED
@@ -35,6 +35,15 @@ Please see the [project documentation](https://socketry.github.io/async/) for mo
35
35
 
36
36
  Please see the [project releases](https://socketry.github.io/async/releases/index) for all releases.
37
37
 
38
+ ### v2.36.0
39
+
40
+ - Introduce `Task#wait_all` which recursively waits for all children and self, excepting the current task.
41
+ - Introduce `Task#join` as an alias for `Task#wait` for compatibility with `Thread#join` and similar interfaces.
42
+
43
+ ### v2.35.3
44
+
45
+ - `Async::Clock` now implements `#as_json` and `#to_json` for nicer log formatting.
46
+
38
47
  ### v2.35.2
39
48
 
40
49
  - Improved handling of `Process.fork` on Ruby 4+.
@@ -68,23 +77,6 @@ Please see the [project releases](https://socketry.github.io/async/releases/inde
68
77
 
69
78
  - Introduce `Async::Deadline` for precise timeout management in compound operations.
70
79
 
71
- ### v2.30.0
72
-
73
- - Add timeout support to `Async::Queue#dequeue` and `Async::Queue#pop` methods.
74
- - Add timeout support to `Async::PriorityQueue#dequeue` and `Async::PriorityQueue#pop` methods.
75
- - Add `closed?` method to `Async::PriorityQueue` for full queue interface compatibility.
76
- - Support non-blocking operations using `timeout: 0` parameter.
77
-
78
- ### v2.29.0
79
-
80
- This release introduces thread-safety as a core concept of Async. Many core classes now have thread-safe guarantees, allowing them to be used safely across multiple threads.
81
-
82
- - Thread-safe `Async::Condition` and `Async::Notification`, implemented using `Thread::Queue`.
83
- - Thread-safe `Async::Queue` and `Async::LimitedQueue`, implemented using `Thread::Queue` and `Thread::LimitedQueue` respectively.
84
- - `Async::Variable` is deprecated in favor of `Async::Promise`.
85
- - [Introduce `Async::Promise`](https://socketry.github.io/async/releases/index#introduce-async::promise)
86
- - [Introduce `Async::PriorityQueue`](https://socketry.github.io/async/releases/index#introduce-async::priorityqueue)
87
-
88
80
  ## See Also
89
81
 
90
82
  - [async-http](https://github.com/socketry/async-http) — Asynchronous HTTP client/server.
data/releases.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Releases
2
2
 
3
+ ## v2.36.0
4
+
5
+ - Introduce `Task#wait_all` which recursively waits for all children and self, excepting the current task.
6
+ - Introduce `Task#join` as an alias for `Task#wait` for compatibility with `Thread#join` and similar interfaces.
7
+
8
+ ## v2.35.3
9
+
10
+ - `Async::Clock` now implements `#as_json` and `#to_json` for nicer log formatting.
11
+
3
12
  ## v2.35.2
4
13
 
5
14
  - Improved handling of `Process.fork` on Ruby 4+.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.35.2
4
+ version: 2.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -175,7 +175,6 @@ files:
175
175
  - lib/async/semaphore.md
176
176
  - lib/async/semaphore.rb
177
177
  - lib/async/stop.rb
178
- - lib/async/task.md
179
178
  - lib/async/task.rb
180
179
  - lib/async/timeout.rb
181
180
  - lib/async/variable.rb
metadata.gz.sig CHANGED
Binary file
data/lib/async/task.md DELETED
@@ -1,30 +0,0 @@
1
- A sequence of instructions, defined by a block, which is executed sequentially and managed by the scheduler. A task can be in one of the following states: `initialized`, `running`, `completed`, `failed`, `cancelled` or `stopped`.
2
-
3
- ```mermaid
4
- stateDiagram-v2
5
- [*] --> Initialized
6
- Initialized --> Running : Run
7
-
8
- Running --> Completed : Return Value
9
- Running --> Failed : Exception
10
-
11
- Completed --> [*]
12
- Failed --> [*]
13
-
14
- Running --> Stopped : Stop
15
- Stopped --> [*]
16
- Completed --> Stopped : Stop
17
- Failed --> Stopped : Stop
18
- Initialized --> Stopped : Stop
19
- ```
20
-
21
- ## Example
22
-
23
- ```ruby
24
- require "async"
25
-
26
- # Create an asynchronous task that sleeps for 1 second:
27
- Async do |task|
28
- sleep(1)
29
- end
30
- ```