From eba807df1b478b6c168bc1e64758fdad3d97c6a0 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Sun, 30 Aug 2026 11:15:15 +0200 Subject: [PATCH] Honor Ractor callbacks in direct mode --- lib/parallel.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/parallel.rb b/lib/parallel.rb index 710f241..f5ab604 100644 --- a/lib/parallel.rb +++ b/lib/parallel.rb @@ -290,6 +290,12 @@ def map(source, options = {}, &block) job_factory = JobFactory.new(source, options[:mutex]) size = [job_factory.size, size].min + invalid_ractor_call = block && options[:ractor] + invalid_ractor_call ||= size > 0 && !options[:ractor] + if method == :in_ractors && invalid_ractor_call + raise ArgumentError, "pass either a block for direct execution or `ractor: [ClassName, :method_name]`" + end + options[:return_results] = (options[:preserve_results] != false || !!options[:finish]) add_progress_bar!(job_factory, options) @@ -428,7 +434,11 @@ def work_direct(job_factory, options, &block) while (set = job_factory.next) item, index = set results << with_instrumentation(item, index, options) do - call_with_index(item, index, options, &block) + if (callback = options[:ractor]) + call_ractor_callback(callback, item, index, options) + else + call_with_index(item, index, options, &block) + end end end rescue StandardError @@ -470,9 +480,6 @@ def work_in_ractors(job_factory, options) results_mutex = Mutex.new # arrays are not thread-safe on jRuby callback = options[:ractor] - if block_given? || !callback - raise ArgumentError, "pass the code you want to execute as `ractor: [ClassName, :method_name]`" - end use_port = defined?(Ractor::Port) @@ -700,6 +707,13 @@ def call_with_index(item, index, options, &block) end end + def call_ractor_callback(callback, item, index, options) + klass, method_name = callback + args = [item] + args << index if options[:with_index] + klass.send(method_name, *args) + end + def with_instrumentation(item, index, options) instrument_start(item, index, options) result = yield