Aller au contenu

CPU-bound thread under GIL (ARCH6042)

Ce contenu n’est pas encore disponible dans votre langue.

Key ruby.async.cpu-bound-thread-gil · Severity info · Category async

In GIL-constrained runtimes such as MRI Ruby, threads do not parallelize CPU work - only one runs Ruby code at a time. Spreading CPU-bound work across threads adds overhead without speedup. Use processes or Ractors instead.

threads = chunks.map do |chunk|
Thread.new { chunk.map { |n| fibonacci(n) } }
end
results = threads.flat_map(&:value)
require "etc"
results = Parallel.map(chunks, in_processes: Etc.nprocessors) do |chunk|
chunk.map { |n| fibonacci(n) }
end

Parallelize CPU-bound work with separate processes (e.g. Parallel.map in_processes:) or Ractors instead of threads.