async 2.18.0 → 2.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/async/barrier.rb +2 -2
- data/lib/async/clock.rb +1 -1
- data/lib/async/condition.rb +1 -1
- data/lib/async/console.rb +42 -0
- data/lib/async/idler.rb +2 -1
- data/lib/async/notification.rb +1 -1
- data/lib/async/queue.rb +2 -2
- data/lib/async/scheduler.rb +101 -24
- data/lib/async/semaphore.rb +1 -1
- data/lib/async/task.rb +20 -7
- data/lib/async/version.rb +1 -1
- data/lib/async/wrapper.rb +2 -0
- data/lib/kernel/async.rb +1 -1
- data/lib/kernel/sync.rb +1 -1
- data/lib/metrics/provider/async/task.rb +17 -0
- data/lib/metrics/provider/async.rb +6 -0
- data/lib/traces/provider/async/barrier.rb +17 -0
- data/lib/traces/provider/async/task.rb +29 -0
- data/lib/traces/provider/async.rb +7 -0
- data/readme.md +9 -0
- data/releases.md +51 -0
- data.tar.gz.sig +0 -0
- metadata +11 -5
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 920118919742122dff3aac0288b03f26a34ede0fddbe1cca028782061780d6b3
|
4
|
+
data.tar.gz: 9e292ef0a389ec607b9fdbbf86d64ea68cfc44e040eda784e067211ff3cd7b79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a56d10905b2165753f18c80d6763e58fadf575ccddc4aca391082e7a354a131cdd7edcb635cd09927a216049d4051f2947d777f496e4bed87ea6bb64143fbecd
|
7
|
+
data.tar.gz: 787cdea5aef8460d8a410216c56550210fd4639586c6b8c89e9190a247d979171da45054d83516f3b6f864ba0b202d1b581bc16cc0653ed853a20342363b3982
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/async/barrier.rb
CHANGED
@@ -9,11 +9,11 @@ require_relative "task"
|
|
9
9
|
module Async
|
10
10
|
# A general purpose synchronisation primitive, which allows one task to wait for a number of other tasks to complete. It can be used in conjunction with {Semaphore}.
|
11
11
|
#
|
12
|
-
# @public Since
|
12
|
+
# @public Since *Async v1*.
|
13
13
|
class Barrier
|
14
14
|
# Initialize the barrier.
|
15
15
|
# @parameter parent [Task | Semaphore | Nil] The parent for holding any children tasks.
|
16
|
-
# @public Since
|
16
|
+
# @public Since *Async v1*.
|
17
17
|
def initialize(parent: nil)
|
18
18
|
@tasks = List.new
|
19
19
|
|
data/lib/async/clock.rb
CHANGED
data/lib/async/condition.rb
CHANGED
@@ -9,7 +9,7 @@ require_relative "list"
|
|
9
9
|
|
10
10
|
module Async
|
11
11
|
# A synchronization primitive, which allows fibers to wait until a particular condition is (edge) triggered.
|
12
|
-
# @public Since
|
12
|
+
# @public Since *Async v1*.
|
13
13
|
class Condition
|
14
14
|
# Create a new condition.
|
15
15
|
def initialize
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2024, by Samuel Williams.
|
5
|
+
|
6
|
+
module Async
|
7
|
+
# Shims for the console gem, redirecting warnings and above to `Kernel#warn`.
|
8
|
+
#
|
9
|
+
# If you require this file, the `async` library will not depend on the `console` gem.
|
10
|
+
#
|
11
|
+
# That includes any gems that sit within the `Async` namespace.
|
12
|
+
#
|
13
|
+
# This is an experimental feature.
|
14
|
+
module Console
|
15
|
+
# Log a message at the debug level. The shim is silent.
|
16
|
+
def self.debug(...)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Log a message at the info level. The shim is silent.
|
20
|
+
def self.info(...)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Log a message at the warn level. The shim redirects to `Kernel#warn`.
|
24
|
+
def self.warn(*arguments, exception: nil, **options)
|
25
|
+
if exception
|
26
|
+
super(*arguments, exception.full_message, **options)
|
27
|
+
else
|
28
|
+
super(*arguments, **options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Log a message at the error level. The shim redirects to `Kernel#warn`.
|
33
|
+
def self.error(...)
|
34
|
+
self.warn(...)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Log a message at the fatal level. The shim redirects to `Kernel#warn`.
|
38
|
+
def self.fatal(...)
|
39
|
+
self.warn(...)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/async/idler.rb
CHANGED
@@ -7,7 +7,8 @@ module Async
|
|
7
7
|
# A load balancing mechanism that can be used process work when the system is idle.
|
8
8
|
class Idler
|
9
9
|
# Create a new idler.
|
10
|
-
#
|
10
|
+
#
|
11
|
+
# @public Since *Async v2*.
|
11
12
|
#
|
12
13
|
# @parameter maximum_load [Numeric] The maximum load before we start shedding work.
|
13
14
|
# @parameter backoff [Numeric] The initial backoff time, used for delaying work.
|
data/lib/async/notification.rb
CHANGED
@@ -7,7 +7,7 @@ require_relative "condition"
|
|
7
7
|
|
8
8
|
module Async
|
9
9
|
# A synchronization primitive, which allows fibers to wait until a notification is received. Does not block the task which signals the notification. Waiting tasks are resumed on next iteration of the reactor.
|
10
|
-
# @public Since
|
10
|
+
# @public Since *Async v1*.
|
11
11
|
class Notification < Condition
|
12
12
|
# Signal to a given task that it should resume operations.
|
13
13
|
def signal(value = nil, task: Task.current)
|
data/lib/async/queue.rb
CHANGED
@@ -12,7 +12,7 @@ module Async
|
|
12
12
|
#
|
13
13
|
# It has a compatible interface with {Notification} and {Condition}, except that it's multi-value.
|
14
14
|
#
|
15
|
-
# @public Since
|
15
|
+
# @public Since *Async v1*.
|
16
16
|
class Queue
|
17
17
|
# Create a new queue.
|
18
18
|
#
|
@@ -99,7 +99,7 @@ module Async
|
|
99
99
|
end
|
100
100
|
|
101
101
|
# A queue which limits the number of items that can be enqueued.
|
102
|
-
# @public Since
|
102
|
+
# @public Since *Async v1*.
|
103
103
|
class LimitedQueue < Queue
|
104
104
|
# Create a new limited queue.
|
105
105
|
#
|
data/lib/async/scheduler.rb
CHANGED
@@ -27,14 +27,14 @@ module Async
|
|
27
27
|
end
|
28
28
|
|
29
29
|
# Whether the fiber scheduler is supported.
|
30
|
-
# @public Since
|
30
|
+
# @public Since *Async v1*.
|
31
31
|
def self.supported?
|
32
32
|
true
|
33
33
|
end
|
34
34
|
|
35
35
|
# Create a new scheduler.
|
36
36
|
#
|
37
|
-
# @public Since
|
37
|
+
# @public Since *Async v1*.
|
38
38
|
# @parameter parent [Node | Nil] The parent node to use for task hierarchy.
|
39
39
|
# @parameter selector [IO::Event::Selector] The selector to use for event handling.
|
40
40
|
def initialize(parent = nil, selector: nil)
|
@@ -52,6 +52,7 @@ module Async
|
|
52
52
|
end
|
53
53
|
|
54
54
|
# Compute the scheduler load according to the busy and idle times that are updated by the run loop.
|
55
|
+
#
|
55
56
|
# @returns [Float] The load of the scheduler. 0.0 means no load, 1.0 means fully loaded or over-loaded.
|
56
57
|
def load
|
57
58
|
total_time = @busy_time + @idle_time
|
@@ -95,7 +96,7 @@ module Async
|
|
95
96
|
end
|
96
97
|
|
97
98
|
# Terminate all child tasks and close the scheduler.
|
98
|
-
# @public Since
|
99
|
+
# @public Since *Async v1*.
|
99
100
|
def close
|
100
101
|
self.run_loop do
|
101
102
|
until self.terminate
|
@@ -115,7 +116,7 @@ module Async
|
|
115
116
|
end
|
116
117
|
|
117
118
|
# @returns [Boolean] Whether the scheduler has been closed.
|
118
|
-
# @public Since
|
119
|
+
# @public Since *Async v1*.
|
119
120
|
def closed?
|
120
121
|
@selector.nil?
|
121
122
|
end
|
@@ -167,6 +168,8 @@ module Async
|
|
167
168
|
end
|
168
169
|
|
169
170
|
# Invoked when a fiber tries to perform a blocking operation which cannot continue. A corresponding call {unblock} must be performed to allow this fiber to continue.
|
171
|
+
#
|
172
|
+
|
170
173
|
# @asynchronous May only be called on same thread as fiber scheduler.
|
171
174
|
def block(blocker, timeout)
|
172
175
|
# $stderr.puts "block(#{blocker}, #{Fiber.current}, #{timeout})"
|
@@ -190,7 +193,13 @@ module Async
|
|
190
193
|
timer&.cancel!
|
191
194
|
end
|
192
195
|
|
196
|
+
# Unblock a fiber that was previously blocked.
|
197
|
+
#
|
198
|
+
# @public Since *Async v2* and *Ruby v3.1*.
|
193
199
|
# @asynchronous May be called from any thread.
|
200
|
+
#
|
201
|
+
# @parameter blocker [Object] The object that was blocking the fiber.
|
202
|
+
# @parameter fiber [Fiber] The fiber to unblock.
|
194
203
|
def unblock(blocker, fiber)
|
195
204
|
# $stderr.puts "unblock(#{blocker}, #{fiber})"
|
196
205
|
|
@@ -201,7 +210,12 @@ module Async
|
|
201
210
|
end
|
202
211
|
end
|
203
212
|
|
204
|
-
#
|
213
|
+
# Sleep for the specified duration.
|
214
|
+
#
|
215
|
+
# @public Since *Async v2* and *Ruby v3.1*.
|
216
|
+
# @asynchronous May be non-blocking.
|
217
|
+
#
|
218
|
+
# @parameter duration [Numeric | Nil] The time in seconds to sleep, or if nil, indefinitely.
|
205
219
|
def kernel_sleep(duration = nil)
|
206
220
|
if duration
|
207
221
|
self.block(nil, duration)
|
@@ -210,7 +224,12 @@ module Async
|
|
210
224
|
end
|
211
225
|
end
|
212
226
|
|
213
|
-
#
|
227
|
+
# Resolve the address of the given hostname.
|
228
|
+
#
|
229
|
+
# @public Since *Async v2*.
|
230
|
+
# @asynchronous May be non-blocking.
|
231
|
+
#
|
232
|
+
# @parameter hostname [String] The hostname to resolve.
|
214
233
|
def address_resolve(hostname)
|
215
234
|
# On some platforms, hostnames may contain a device-specific suffix (e.g. %en0). We need to strip this before resolving.
|
216
235
|
# See <https://github.com/socketry/async/issues/180> for more details.
|
@@ -218,7 +237,6 @@ module Async
|
|
218
237
|
::Resolv.getaddresses(hostname)
|
219
238
|
end
|
220
239
|
|
221
|
-
|
222
240
|
if IO.method_defined?(:timeout)
|
223
241
|
private def get_timeout(io)
|
224
242
|
io.timeout
|
@@ -229,7 +247,14 @@ module Async
|
|
229
247
|
end
|
230
248
|
end
|
231
249
|
|
232
|
-
#
|
250
|
+
# Wait for the specified IO to become ready for the specified events.
|
251
|
+
#
|
252
|
+
# @public Since *Async v2*.
|
253
|
+
# @asynchronous May be non-blocking.
|
254
|
+
#
|
255
|
+
# @parameter io [IO] The IO object to wait on.
|
256
|
+
# @parameter events [Integer] The events to wait for, e.g. `IO::READABLE`, `IO::WRITABLE`, etc.
|
257
|
+
# @parameter timeout [Float | Nil] The maximum time to wait, or if nil, indefinitely.
|
233
258
|
def io_wait(io, events, timeout = nil)
|
234
259
|
fiber = Fiber.current
|
235
260
|
|
@@ -251,6 +276,15 @@ module Async
|
|
251
276
|
end
|
252
277
|
|
253
278
|
if ::IO::Event::Support.buffer?
|
279
|
+
# Read from the specified IO into the buffer.
|
280
|
+
#
|
281
|
+
# @public Since *Async v2* and Ruby with `IO::Buffer` support.
|
282
|
+
# @asynchronous May be non-blocking.
|
283
|
+
#
|
284
|
+
# @parameter io [IO] The IO object to read from.
|
285
|
+
# @parameter buffer [IO::Buffer] The buffer to read into.
|
286
|
+
# @parameter length [Integer] The minimum number of bytes to read.
|
287
|
+
# @parameter offset [Integer] The offset within the buffer to read into.
|
254
288
|
def io_read(io, buffer, length, offset = 0)
|
255
289
|
fiber = Fiber.current
|
256
290
|
|
@@ -266,6 +300,15 @@ module Async
|
|
266
300
|
end
|
267
301
|
|
268
302
|
if RUBY_ENGINE != "ruby" || RUBY_VERSION >= "3.3.1"
|
303
|
+
# Write the specified buffer to the IO.
|
304
|
+
#
|
305
|
+
# @public Since *Async v2* and *Ruby v3.3.1* with `IO::Buffer` support.
|
306
|
+
# @asynchronous May be non-blocking.
|
307
|
+
#
|
308
|
+
# @parameter io [IO] The IO object to write to.
|
309
|
+
# @parameter buffer [IO::Buffer] The buffer to write from.
|
310
|
+
# @parameter length [Integer] The minimum number of bytes to write.
|
311
|
+
# @parameter offset [Integer] The offset within the buffer to write from.
|
269
312
|
def io_write(io, buffer, length, offset = 0)
|
270
313
|
fiber = Fiber.current
|
271
314
|
|
@@ -283,6 +326,10 @@ module Async
|
|
283
326
|
end
|
284
327
|
|
285
328
|
# Wait for the specified process ID to exit.
|
329
|
+
#
|
330
|
+
# @public Since *Async v2*.
|
331
|
+
# @asynchronous May be non-blocking.
|
332
|
+
#
|
286
333
|
# @parameter pid [Integer] The process ID to wait for.
|
287
334
|
# @parameter flags [Integer] A bit-mask of flags suitable for `Process::Status.wait`.
|
288
335
|
# @returns [Process::Status] A process status instance.
|
@@ -335,7 +382,10 @@ module Async
|
|
335
382
|
end
|
336
383
|
|
337
384
|
# Run one iteration of the event loop.
|
338
|
-
#
|
385
|
+
#
|
386
|
+
# @public Since *Async v1*.
|
387
|
+
# @asynchronous Must be invoked from blocking (root) fiber.
|
388
|
+
#
|
339
389
|
# @parameter timeout [Float | Nil] The maximum timeout, or if nil, indefinite.
|
340
390
|
# @returns [Boolean] Whether there is more work to do.
|
341
391
|
def run_once(timeout = nil)
|
@@ -354,6 +404,7 @@ module Async
|
|
354
404
|
end
|
355
405
|
|
356
406
|
# Checks and clears the interrupted state of the scheduler.
|
407
|
+
#
|
357
408
|
# @returns [Boolean] Whether the reactor has been interrupted.
|
358
409
|
private def interrupted?
|
359
410
|
if @interrupted
|
@@ -368,7 +419,9 @@ module Async
|
|
368
419
|
return false
|
369
420
|
end
|
370
421
|
|
371
|
-
# Stop all children, including transient children
|
422
|
+
# Stop all children, including transient children.
|
423
|
+
#
|
424
|
+
# @public Since *Async v1*.
|
372
425
|
def stop
|
373
426
|
@children&.each do |child|
|
374
427
|
child.stop
|
@@ -387,7 +440,13 @@ module Async
|
|
387
440
|
end
|
388
441
|
end
|
389
442
|
rescue Interrupt => interrupt
|
443
|
+
# If an interrupt did occur during an iteration of the event loop, we need to handle it. More specifically, `self.stop` is not safe to interrupt without potentially corrupting the task tree.
|
390
444
|
Thread.handle_interrupt(::SignalException => :never) do
|
445
|
+
Console.debug(self) do |buffer|
|
446
|
+
buffer.puts "Scheduler interrupted: #{interrupt.inspect}"
|
447
|
+
self.print_hierarchy(buffer)
|
448
|
+
end
|
449
|
+
|
391
450
|
self.stop
|
392
451
|
end
|
393
452
|
|
@@ -395,10 +454,19 @@ module Async
|
|
395
454
|
end
|
396
455
|
|
397
456
|
# If the event loop was interrupted, and we finished exiting normally (due to the interrupt), we need to re-raise the interrupt so that the caller can handle it too.
|
398
|
-
|
457
|
+
if interrupt
|
458
|
+
Kernel.raise(interrupt)
|
459
|
+
end
|
399
460
|
end
|
400
461
|
|
401
462
|
# Run the reactor until all tasks are finished. Proxies arguments to {#async} immediately before entering the loop, if a block is provided.
|
463
|
+
#
|
464
|
+
# Forwards all parameters to {#async} if a block is given.
|
465
|
+
#
|
466
|
+
# @public Since *Async v1*.
|
467
|
+
#
|
468
|
+
# @yields {|task| ...} The top level task, if a block is given.
|
469
|
+
# @returns [Task] The initial task that was scheduled into the reactor.
|
402
470
|
def run(...)
|
403
471
|
Kernel.raise ClosedError if @selector.nil?
|
404
472
|
|
@@ -411,30 +479,23 @@ module Async
|
|
411
479
|
return initial_task
|
412
480
|
end
|
413
481
|
|
414
|
-
# Start an asynchronous task within the specified reactor. The task will be
|
415
|
-
# executed until the first blocking call, at which point it will yield and
|
416
|
-
# and this method will return.
|
482
|
+
# Start an asynchronous task within the specified reactor. The task will be executed until the first blocking call, at which point it will yield and and this method will return.
|
417
483
|
#
|
418
|
-
#
|
484
|
+
# @public Since *Async v1*.
|
485
|
+
# @asynchronous May context switch immediately to new task.
|
486
|
+
# @deprecated Use {#run} or {Task#async} instead.
|
419
487
|
#
|
420
488
|
# @yields {|task| ...} Executed within the task.
|
421
489
|
# @returns [Task] The task that was scheduled into the reactor.
|
422
|
-
# @deprecated With no replacement.
|
423
490
|
def async(*arguments, **options, &block)
|
491
|
+
# warn "Async::Scheduler#async is deprecated. Use `run` or `Task#async` instead.", uplevel: 1, category: :deprecated
|
492
|
+
|
424
493
|
Kernel.raise ClosedError if @selector.nil?
|
425
494
|
|
426
495
|
task = Task.new(Task.current? || self, **options, &block)
|
427
496
|
|
428
|
-
# I want to take a moment to explain the logic of this.
|
429
|
-
# When calling an async block, we deterministically execute it until the
|
430
|
-
# first blocking operation. We don't *have* to do this - we could schedule
|
431
|
-
# it for later execution, but it's useful to:
|
432
|
-
# - Fail at the point of the method call where possible.
|
433
|
-
# - Execute determinstically where possible.
|
434
|
-
# - Avoid scheduler overhead if no blocking operation is performed.
|
435
497
|
task.run(*arguments)
|
436
498
|
|
437
|
-
# Console.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..."
|
438
499
|
return task
|
439
500
|
end
|
440
501
|
|
@@ -443,7 +504,14 @@ module Async
|
|
443
504
|
end
|
444
505
|
|
445
506
|
# Invoke the block, but after the specified timeout, raise {TimeoutError} in any currenly blocking operation. If the block runs to completion before the timeout occurs or there are no non-blocking operations after the timeout expires, the code will complete without any exception.
|
507
|
+
#
|
508
|
+
# @public Since *Async v1*.
|
509
|
+
# @asynchronous May raise an exception at any interruption point (e.g. blocking operations).
|
510
|
+
#
|
446
511
|
# @parameter duration [Numeric] The time in seconds, in which the task should complete.
|
512
|
+
# @parameter exception [Class] The exception class to raise.
|
513
|
+
# @parameter message [String] The message to pass to the exception.
|
514
|
+
# @yields {|duration| ...} The block to execute with a timeout.
|
447
515
|
def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block)
|
448
516
|
fiber = Fiber.current
|
449
517
|
|
@@ -458,6 +526,15 @@ module Async
|
|
458
526
|
timer&.cancel!
|
459
527
|
end
|
460
528
|
|
529
|
+
# Invoke the block, but after the specified timeout, raise the specified exception with the given message. If the block runs to completion before the timeout occurs or there are no non-blocking operations after the timeout expires, the code will complete without any exception.
|
530
|
+
#
|
531
|
+
# @public Since *Async v1* and *Ruby v3.1*. May be invoked from `Timeout.timeout`.
|
532
|
+
# @asynchronous May raise an exception at any interruption point (e.g. blocking operations).
|
533
|
+
#
|
534
|
+
# @parameter duration [Numeric] The time in seconds, in which the task should complete.
|
535
|
+
# @parameter exception [Class] The exception class to raise.
|
536
|
+
# @parameter message [String] The message to pass to the exception.
|
537
|
+
# @yields {|duration| ...} The block to execute with a timeout.
|
461
538
|
def timeout_after(duration, exception, message, &block)
|
462
539
|
with_timeout(duration, exception, message) do |timer|
|
463
540
|
yield duration
|
data/lib/async/semaphore.rb
CHANGED
@@ -7,7 +7,7 @@ require_relative "list"
|
|
7
7
|
|
8
8
|
module Async
|
9
9
|
# A synchronization primitive, which limits access to a given resource.
|
10
|
-
# @public Since
|
10
|
+
# @public Since *Async v1*.
|
11
11
|
class Semaphore
|
12
12
|
# @parameter limit [Integer] The maximum number of times the semaphore can be acquired before it blocks.
|
13
13
|
# @parameter parent [Task | Semaphore | Nil] The parent for holding any children tasks.
|
data/lib/async/task.rb
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
# Copyright, 2023, by Math Ieu.
|
9
9
|
|
10
10
|
require "fiber"
|
11
|
-
require "console
|
11
|
+
require "console"
|
12
12
|
|
13
13
|
require_relative "node"
|
14
14
|
require_relative "condition"
|
@@ -40,7 +40,7 @@ module Async
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# Raised if a timeout occurs on a specific Fiber. Handled gracefully by `Task`.
|
43
|
-
# @public Since
|
43
|
+
# @public Since *Async v1*.
|
44
44
|
class TimeoutError < StandardError
|
45
45
|
# Create a new timeout error.
|
46
46
|
#
|
@@ -50,7 +50,7 @@ module Async
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
# @public Since
|
53
|
+
# @public Since *Async v1*.
|
54
54
|
class Task < Node
|
55
55
|
# Raised when a child task is created within a task that has finished execution.
|
56
56
|
class FinishedError < RuntimeError
|
@@ -198,9 +198,7 @@ module Async
|
|
198
198
|
rescue => error
|
199
199
|
# I'm not completely happy with this overhead, but the alternative is to not log anything which makes debugging extremely difficult. Maybe we can introduce a debug wrapper which adds extra logging.
|
200
200
|
if @finished.nil?
|
201
|
-
|
202
|
-
else
|
203
|
-
# Console::Event::Failure.for(error).emit(self, severity: :debug)
|
201
|
+
warn(self, "Task may have ended with unhandled exception.", exception: error)
|
204
202
|
end
|
205
203
|
|
206
204
|
raise
|
@@ -212,6 +210,10 @@ module Async
|
|
212
210
|
|
213
211
|
# Run an asynchronous task as a child of the current task.
|
214
212
|
#
|
213
|
+
# @public Since *Async v1*.
|
214
|
+
# @asynchronous May context switch immediately to the new task.
|
215
|
+
#
|
216
|
+
# @yields {|task| ...} in the context of the new task.
|
215
217
|
# @raises [FinishedError] If the task has already finished.
|
216
218
|
# @returns [Task] The child task.
|
217
219
|
def async(*arguments, **options, &block)
|
@@ -219,6 +221,13 @@ module Async
|
|
219
221
|
|
220
222
|
task = Task.new(self, **options, &block)
|
221
223
|
|
224
|
+
# When calling an async block, we deterministically execute it until the first blocking operation. We don't *have* to do this - we could schedule it for later execution, but it's useful to:
|
225
|
+
#
|
226
|
+
# - Fail at the point of the method call where possible.
|
227
|
+
# - Execute determinstically where possible.
|
228
|
+
# - Avoid scheduler overhead if no blocking operation is performed.
|
229
|
+
#
|
230
|
+
# There are different strategies (greedy vs non-greedy). We are currently using a greedy strategy.
|
222
231
|
task.run(*arguments)
|
223
232
|
|
224
233
|
return task
|
@@ -304,7 +313,7 @@ module Async
|
|
304
313
|
# If stop is invoked a second time, it will be immediately executed.
|
305
314
|
#
|
306
315
|
# @yields {} The block of code to execute.
|
307
|
-
# @public Since
|
316
|
+
# @public Since *Async v1*.
|
308
317
|
def defer_stop
|
309
318
|
# Tri-state variable for controlling stop:
|
310
319
|
# - nil: defer_stop has not been called.
|
@@ -362,6 +371,10 @@ module Async
|
|
362
371
|
|
363
372
|
private
|
364
373
|
|
374
|
+
def warn(...)
|
375
|
+
Console.warn(...)
|
376
|
+
end
|
377
|
+
|
365
378
|
# Finish the current task, moving any children to the parent.
|
366
379
|
def finish!
|
367
380
|
# Don't hold references to the fiber or block after the task has finished:
|
data/lib/async/version.rb
CHANGED
data/lib/async/wrapper.rb
CHANGED
@@ -4,6 +4,8 @@
|
|
4
4
|
# Copyright, 2017-2024, by Samuel Williams.
|
5
5
|
# Copyright, 2017, by Kent Gruber.
|
6
6
|
|
7
|
+
warn "Async::Wrapper is deprecated and will be removed on 2025-03-31. Please use native interfaces instead.", uplevel: 1, category: :deprecated
|
8
|
+
|
7
9
|
module Async
|
8
10
|
# Represents an asynchronous IO within a reactor.
|
9
11
|
# @deprecated With no replacement. Prefer native interfaces.
|
data/lib/kernel/async.rb
CHANGED
@@ -19,7 +19,7 @@ module Kernel
|
|
19
19
|
# @yields {|task| ...} The block that will execute asynchronously.
|
20
20
|
# @parameter task [Async::Task] The task that is executing the given block.
|
21
21
|
#
|
22
|
-
# @public Since
|
22
|
+
# @public Since *Async v1*.
|
23
23
|
# @asynchronous May block until given block completes executing.
|
24
24
|
def Async(...)
|
25
25
|
if current = ::Async::Task.current?
|
data/lib/kernel/sync.rb
CHANGED
@@ -14,7 +14,7 @@ module Kernel
|
|
14
14
|
# @yields {|task| ...} The block that will execute asynchronously.
|
15
15
|
# @parameter task [Async::Task] The task that is executing the given block.
|
16
16
|
#
|
17
|
-
# @public Since
|
17
|
+
# @public Since *Async v1*.
|
18
18
|
# @asynchronous Will block until given block completes executing.
|
19
19
|
def Sync(annotation: nil, &block)
|
20
20
|
if task = ::Async::Task.current?
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2024, by Samuel Williams.
|
5
|
+
|
6
|
+
require_relative "../../../async/task"
|
7
|
+
require "metrics/provider"
|
8
|
+
|
9
|
+
Metrics::Provider(Async::Task) do
|
10
|
+
ASYNC_TASK_SCHEDULED = Metrics.metric("async.task.scheduled", :counter, description: "The number of tasks scheduled.")
|
11
|
+
|
12
|
+
def schedule(&block)
|
13
|
+
ASYNC_TASK_SCHEDULED.emit(1)
|
14
|
+
|
15
|
+
super(&block)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2022, by Samuel Williams.
|
5
|
+
|
6
|
+
require_relative "../../../async/barrier"
|
7
|
+
require "traces/provider"
|
8
|
+
|
9
|
+
Traces::Provider(Async::Barrier) do
|
10
|
+
def wait
|
11
|
+
attributes = {
|
12
|
+
"size" => self.size
|
13
|
+
}
|
14
|
+
|
15
|
+
Traces.trace("async.barrier.wait", attributes: attributes) {super}
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2022, by Samuel Williams.
|
5
|
+
|
6
|
+
require_relative "../../../async/task"
|
7
|
+
require "traces/provider"
|
8
|
+
|
9
|
+
Traces::Provider(Async::Task) do
|
10
|
+
def schedule(&block)
|
11
|
+
unless self.transient?
|
12
|
+
trace_context = Traces.trace_context
|
13
|
+
end
|
14
|
+
|
15
|
+
super do
|
16
|
+
Traces.trace_context = trace_context
|
17
|
+
|
18
|
+
if annotation = self.annotation
|
19
|
+
attributes = {
|
20
|
+
"annotation" => annotation
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
Traces.trace("async.task", attributes: attributes) do
|
25
|
+
yield
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
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.20.0
|
39
|
+
|
40
|
+
- [Traces and Metrics Providers](https://socketry.github.io/async/releases/index#traces-and-metrics-providers)
|
41
|
+
|
42
|
+
### v2.19.0
|
43
|
+
|
44
|
+
- [Async::Scheduler Debugging](https://socketry.github.io/async/releases/index#async::scheduler-debugging)
|
45
|
+
- [Console Shims](https://socketry.github.io/async/releases/index#console-shims)
|
46
|
+
|
38
47
|
### v2.18.0
|
39
48
|
|
40
49
|
- Add support for `Sync(annotation:)`, so that you can annotate the block with a description of what it does, even if it doesn't create a new task.
|
data/releases.md
CHANGED
@@ -1,5 +1,56 @@
|
|
1
1
|
# Releases
|
2
2
|
|
3
|
+
## v2.20.0
|
4
|
+
|
5
|
+
### Traces and Metrics Providers
|
6
|
+
|
7
|
+
Async now has [traces](https://github.com/socketry/traces) and [metrics](https://github.com/socketry/metrics) providers for various core classes. This allows you to emit traces and metrics to a suitable backend (including DataDog, New Relic, OpenTelemetry, etc.) for monitoring and debugging purposes.
|
8
|
+
|
9
|
+
To take advantage of this feature, you will need to introduce your own `config/traces.rb` and `config/metrics.rb`. Async's own repository includes these files for testing purposes, you could copy them into your own project and modify them as needed.
|
10
|
+
|
11
|
+
## v2.19.0
|
12
|
+
|
13
|
+
### Async::Scheduler Debugging
|
14
|
+
|
15
|
+
Occasionally on issues, I encounter people asking for help and I need more information. Pressing Ctrl-C to exit a hung program is common, but it usually doesn't provide enough information to diagnose the problem. Setting the `CONSOLE_LEVEL=debug` environment variable will now print additional information about the scheduler when you interrupt it, including a backtrace of the current tasks.
|
16
|
+
|
17
|
+
> CONSOLE_LEVEL=debug bundle exec ruby ./test.rb
|
18
|
+
^C 0.0s debug: Async::Reactor [oid=0x974] [ec=0x988] [pid=9116] [2024-11-08 14:12:03 +1300]
|
19
|
+
| Scheduler interrupted: Interrupt
|
20
|
+
| #<Async::Reactor:0x0000000000000974 1 children (running)>
|
21
|
+
| #<Async::Task:0x000000000000099c /Users/samuel/Developer/socketry/async/lib/async/scheduler.rb:185:in `transfer' (running)>
|
22
|
+
| → /Users/samuel/Developer/socketry/async/lib/async/scheduler.rb:185:in `transfer'
|
23
|
+
| /Users/samuel/Developer/socketry/async/lib/async/scheduler.rb:185:in `block'
|
24
|
+
| /Users/samuel/Developer/socketry/async/lib/async/scheduler.rb:207:in `kernel_sleep'
|
25
|
+
| /Users/samuel/Developer/socketry/async/test.rb:7:in `sleep'
|
26
|
+
| /Users/samuel/Developer/socketry/async/test.rb:7:in `sleepy'
|
27
|
+
| /Users/samuel/Developer/socketry/async/test.rb:12:in `block in <top (required)>'
|
28
|
+
| /Users/samuel/Developer/socketry/async/lib/async/task.rb:197:in `block in run'
|
29
|
+
| /Users/samuel/Developer/socketry/async/lib/async/task.rb:420:in `block in schedule'
|
30
|
+
/Users/samuel/Developer/socketry/async/lib/async/scheduler.rb:317:in `select': Interrupt
|
31
|
+
... (backtrace continues) ...
|
32
|
+
|
33
|
+
This gives better visibility into what the scheduler is doing, and should help diagnose issues.
|
34
|
+
|
35
|
+
### Console Shims
|
36
|
+
|
37
|
+
The `async` gem depends on `console` gem, because my goal was to have good logging by default without thinking about it too much. However, some users prefer to avoid using the `console` gem for logging, so I've added an experimental set of shims which should allow you to bypass the `console` gem entirely.
|
38
|
+
|
39
|
+
``` ruby
|
40
|
+
require 'async/console'
|
41
|
+
require 'async'
|
42
|
+
|
43
|
+
Async{raise "Boom"}
|
44
|
+
```
|
45
|
+
|
46
|
+
Will now use `Kernel#warn` to print the task failure warning:
|
47
|
+
|
48
|
+
#<Async::Task:0x00000000000012d4 /home/samuel/Developer/socketry/async/lib/async/task.rb:104:in `backtrace' (running)>
|
49
|
+
Task may have ended with unhandled exception.
|
50
|
+
(irb):4:in `block in <top (required)>': Boom (RuntimeError)
|
51
|
+
from /home/samuel/Developer/socketry/async/lib/async/task.rb:197:in `block in run'
|
52
|
+
from /home/samuel/Developer/socketry/async/lib/async/task.rb:420:in `block in schedule'
|
53
|
+
|
3
54
|
## v2.18.0
|
4
55
|
|
5
56
|
- Add support for `Sync(annotation:)`, so that you can annotate the block with a description of what it does, even if it doesn't create a new task.
|
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.
|
4
|
+
version: 2.20.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -63,7 +63,7 @@ cert_chain:
|
|
63
63
|
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
64
64
|
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
65
65
|
-----END CERTIFICATE-----
|
66
|
-
date: 2024-10
|
66
|
+
date: 2024-11-10 00:00:00.000000000 Z
|
67
67
|
dependencies:
|
68
68
|
- !ruby/object:Gem::Dependency
|
69
69
|
name: console
|
@@ -71,14 +71,14 @@ dependencies:
|
|
71
71
|
requirements:
|
72
72
|
- - "~>"
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
version: '1.
|
74
|
+
version: '1.29'
|
75
75
|
type: :runtime
|
76
76
|
prerelease: false
|
77
77
|
version_requirements: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
79
|
- - "~>"
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
version: '1.
|
81
|
+
version: '1.29'
|
82
82
|
- !ruby/object:Gem::Dependency
|
83
83
|
name: fiber-annotation
|
84
84
|
requirement: !ruby/object:Gem::Requirement
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- lib/async/clock.rb
|
126
126
|
- lib/async/condition.md
|
127
127
|
- lib/async/condition.rb
|
128
|
+
- lib/async/console.rb
|
128
129
|
- lib/async/idler.rb
|
129
130
|
- lib/async/list.rb
|
130
131
|
- lib/async/node.rb
|
@@ -143,6 +144,11 @@ files:
|
|
143
144
|
- lib/async/wrapper.rb
|
144
145
|
- lib/kernel/async.rb
|
145
146
|
- lib/kernel/sync.rb
|
147
|
+
- lib/metrics/provider/async.rb
|
148
|
+
- lib/metrics/provider/async/task.rb
|
149
|
+
- lib/traces/provider/async.rb
|
150
|
+
- lib/traces/provider/async/barrier.rb
|
151
|
+
- lib/traces/provider/async/task.rb
|
146
152
|
- license.md
|
147
153
|
- readme.md
|
148
154
|
- releases.md
|
@@ -168,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
174
|
- !ruby/object:Gem::Version
|
169
175
|
version: '0'
|
170
176
|
requirements: []
|
171
|
-
rubygems_version: 3.
|
177
|
+
rubygems_version: 3.5.22
|
172
178
|
signing_key:
|
173
179
|
specification_version: 4
|
174
180
|
summary: A concurrency framework for Ruby.
|
metadata.gz.sig
CHANGED
Binary file
|