CPU-bound thread under GIL (ARCH6042)
Esta página aún no está disponible en tu idioma.
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.
Incorrect
Section titled “Incorrect”threads = chunks.map do |chunk| Thread.new { chunk.map { |n| fibonacci(n) } }endresults = threads.flat_map(&:value)Correct
Section titled “Correct”require "etc"
results = Parallel.map(chunks, in_processes: Etc.nprocessors) do |chunk| chunk.map { |n| fibonacci(n) }endParallelize CPU-bound work with separate processes (e.g. Parallel.map in_processes:) or Ractors instead of threads.