简体   繁体   中英

Multitasked conversion from attachment_fu to Carrierwave

I have a rake task to convert my photos from attachment_fu to Carrierwave.

Is there a way to run the task :convert multiple times in parallel to make this faster ?

Here is my working task :

namespace :photos do
  task :convert => :environment do
    Item.all.each do |item|
      item.photos.each do |photo|
        new_photo = ItemPhoto.new :photo => File.open(File.join(Rails.root, "public", photo.public_filename)),
                                  :item_id => item.id

        new_photo.save
      end
      item.update_attribute :migrated, true
    end     
  end
end

Take a look at the parallel gem

It could look like this for 8 concurrent processes:

namespace :photos do
  task :convert => :environment do
    Parallel.each(Item.all, :in_processes => 8) do |item|
       item.photos.each do |photo|
          new_photo = ItemPhoto.new :photo => File.open(File.join(Rails.root, "public", photo.public_filename)),
                              :item_id => item.id

          new_photo.save
       end
       item.update_attribute :migrated, true
    end
  end
end

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM