[英]Uploading Multiple Images to Imgur API with Ruby is Slow
因此,我有一个可以一次提交多个图像的表单。 我还用Ruby为Imgur API编写了一个包装器。 我的问题是,由于这是完全同步发生的,因此即使是10张图像,它也要花很长时间甚至超时。 我想知道是否有更好的方法可以处理更多图像。
我所能想到的就是异步提交带有一个图像的表单,但是我不确定这是一个好主意还是会阻止其他请求。
class MissionsController < ApplicationController
def add_images
image_ids = params[:images].collect do |image|
Image.with_imgur(
title: "#{@mission.trip.name} - #{current_user.username}",
image: image,
album_id: @mission.album.imgur_id,
user_id: current_user.id,
trip_id: @mission.trip_id
).imgur_id
end
end
end
class Image < ActiveRecord::Base
def self.with_imgur(options)
trip_id = options.delete(:trip_id)
user_id = options.delete(:user_id)
image = Imgur::Image.create(options)
create(
imgur_id: image["id"],
link: image["link"],
trip_id: trip_id,
user_id: user_id
)
end
end
https://github.com/tomprats/toms-missions/blob/master/app/models/imgur.rb#L116
class Imgur::Image < Base
def self.create(options)
url = "https://api.imgur.com/3/image"
params = {
image: options[:image],
album: options[:album_id],
type: options[:type] || "file", # "file" || "base64" || "URL"
title: options[:title],
description: options[:description]
}
api_post(url, params).body["data"]
end
end
我来看看Typhoeus https://github.com/typhoeus/typhoeus-过去我曾用它来处理到Amazon Glacier的上传等。 我发现它可能会快得多-Typhoeus为您提供了一个未来的对象,它将在后台处理您的上载而不会阻止该应用程序。
我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.