加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用rails执行大脚本

发布时间:2020-12-17 02:00:30 所属栏目:百科 来源:网络整理
导读:我做了一个非常大的脚本,感觉我的初始数据进入我的rails应用程序.我的CSV和10000张图片中有大约3000行. 可能300上传后我得到了这条消息: /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/kernel/agnostics.rb:7:in `
我做了一个非常大的脚本,感觉我的初始数据进入我的rails应用程序.我的CSV和10000张图片中有大约3000行.

可能300上传后我得到了这条消息:

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/kernel/agnostics.rb:7:in ``': Cannot allocate memory - identify -format %wx%h '/tmp/stream20111104-14788-1hsumv7.jpg[0]' (Errno::ENOMEM)

我的上传脚本:

if (row[28] != nil)
   hotelalbum = HotelAlbumPhoto.find_or_create_by_title(h.title)
   hotelalbum.description = "Album photo de l'hotel " + h.title.capitalize
   hotelalbum.hotel_id = h.id
   hotelalbum.save

   files =  Dir.glob('IMAGES/' + row[28].gsub(//,'/') + '/*.jpg')
   i =0
   for file in files
      i += 1
      photopath = File.expand_path('../../import',__FILE__) + '/' + file
      photoname = file.split('/').last
      if (i==1)
        hotelalbum.thumbnail = open(photopath)
        hotelalbum.save
      end
      if (i==1)
        h.thumbnail = open(photopath)
      end
      photo = HotelImage.find_or_create_by_image_file_name_and_hotel_album_photo_id(photoname,hotelalbum.id)
      if (photo.image_file_size == nil || photo.image_file_name != photoname)
          photo.image = open(photopath)
          photo.activated = true
          photo.alt = "Photo de l'hotel " + h.title
          photo.save
      else
         puts photopath + ' already updated'
      end
   end
end

当我使用top命令检查我的内存时,我看到ruby进程在每次上传时使用更多内存.我该怎么办呢?

谢谢你的帮助

ps:我的服务器是一个512Mb内存的虚拟机,一个解决方案是增加这个内存,但我希望找到另一个.

解决方法

我不知道open函数的定义在哪里,但我怀疑我没有看到相应的关闭…

更新更好的想法,将photo.image = open(photopath)更改为photo.image = File.read(photopath)

根据文档,阅读:

Opens the file,optionally seeks to the given offset,then 
returns length bytes (defaulting to the rest of the file). 
read ensures the file is closed before returning.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读