Skip to content

Commit 7d068e4

Browse files
Add support for IO::Event::WorkerPool. (#393)
1 parent f30c2f8 commit 7d068e4

File tree

4 files changed

+27
-247
lines changed

4 files changed

+27
-247
lines changed

async.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
2727

2828
spec.add_dependency "console", "~> 1.29"
2929
spec.add_dependency "fiber-annotation"
30-
spec.add_dependency "io-event", "~> 1.9"
30+
spec.add_dependency "io-event", "~> 1.11"
3131
spec.add_dependency "traces", "~> 0.15"
3232
spec.add_dependency "metrics", "~> 0.12"
3333
end

lib/async/scheduler.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
require_relative "clock"
99
require_relative "task"
1010
require_relative "timeout"
11-
require_relative "worker_pool"
1211

1312
require "io/event"
1413

@@ -45,7 +44,29 @@ def initialize(message = "Scheduler is closed!")
4544
def self.supported?
4645
true
4746
end
47+
48+
# Used to augment the scheduler to add support for blocking operations.
49+
module BlockingOperationWait
50+
# Wait for the given work to be executed.
51+
#
52+
# @public Since *Async v2.21* and *Ruby v3.4*.
53+
# @asynchronous May be non-blocking.
54+
#
55+
# @parameter work [Proc] The work to execute on a background thread.
56+
# @returns [Object] The result of the work.
57+
def blocking_operation_wait(work)
58+
@worker_pool.call(work)
59+
end
60+
end
61+
62+
private_constant :BlockingOperationWait
4863

64+
if ::IO::Event.const_defined?(:WorkerPool)
65+
WorkerPool = ::IO::Event::WorkerPool
66+
else
67+
WorkerPool = nil
68+
end
69+
4970
# Create a new scheduler.
5071
#
5172
# @public Since *Async v1*.
@@ -65,14 +86,15 @@ def initialize(parent = nil, selector: nil, profiler: Profiler&.default, worker_
6586
@idle_time = 0.0
6687

6788
@timers = ::IO::Event::Timers.new
89+
6890
if worker_pool == true
69-
@worker_pool = WorkerPool.new
91+
@worker_pool = WorkerPool&.new
7092
else
7193
@worker_pool = worker_pool
7294
end
73-
95+
7496
if @worker_pool
75-
self.singleton_class.prepend(WorkerPool::BlockingOperationWait)
97+
self.singleton_class.prepend(BlockingOperationWait)
7698
end
7799
end
78100

lib/async/worker_pool.rb

Lines changed: 0 additions & 182 deletions
This file was deleted.

test/async/worker_pool.rb

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)