在ruby中使用多线程,我想确保我的代码是“线程安全的” . 我想生成一个新线程以允许CSV文件上传 .

我当前的控制器代码是:

def upload
   semaphore = Mutex.new 
   semaphore.synchronize{
   threadCSV = Thread.new do
   CSV.foreach(params[:leads].path, headers: true) do |lead|
    Customer.create(email: lead[0], first_name: lead[1], last_name: lead[2])
    end
  end
  }
  redirect_to customers_path
  end
end

我是否有必要使用 .join 等待线程完成以认为这是线程安全的,还是Mutex会处理它?

看起来最后添加 thread.join 将允许我与浏览器中的元素进行交互但不导航它(这是有道理的,因为 thread.join 将等待线程完成处理以避免终止正在执行的任何工作) .