0

この単純な例は jruby で実行しますが、実行されるスレッドは 1 つだけです

require 'benchmark'
require 'celluloid/current'

TIMES = 10

def delay
  sleep 1
  # 40_000_000.times.each{|i| i*i}
end

p 'celluloid: true multithreading?'

class FileWorker
  include Celluloid

  def create_file(id)
    delay
    p "Done!"
    File.open("out_#{id}.txt", 'w') {|f| f.write(Time.now) }
  end
end

workers_pool = FileWorker.pool(size: 10)

TIMES.times do |i|
  # workers_pool.async.create_file(i) # also not happens
  future = Celluloid::Future.new { FileWorker.new.create_file(i) }
  p future.value
end

作成されたすべてのファイルの間隔は 1 秒です。

すべてのファイルが同時に作成されるマルチスレッド モードに Celluloid を変換するのを手伝ってください。

ありがとう!

修繕:

確かに、「先物」の配列が役立ちます!

futures = []
TIMES.times do |i|
   futures << Celluloid::Future.new { FileWorker.new.create_file(i) }
end
futures.each {|f| p f.value }

ありがとうございます

4

1 に答える 1