【发布时间】:2013-12-06 10:56:58
【问题描述】:
我正在尝试使用 Goliath 和 Grape 创建一个非常简单的 Web 服务。我的服务所做的只是给定图像路径和目标尺寸,它将返回图像的新几何形状。图像存储在与 Web 服务主机相同的服务器中。
所以我在 Grape 中有这段代码:
# some Grape code omitted
get "/" do
EM.defer {
image = Magick::Image.read('path to image').first
image.change_geometry('3000x3900') do |cols, row, img|
return {width: cols, height: row}
end
}
end
当我在浏览器中访问端点时,我得到的只是这个字符串
"#<ConditionVariable:0x007ffd9de1f6e8>"
如果没有 EM.defer,它会返回以下 json,但请求/秒非常低(大约 4 个请求/秒):
{width: 'new width', height: 'new heigth'}
如何使 Rmagick 操作非阻塞并返回结果?
【问题讨论】:
-
见eventmachine.rubyforge.org/EventMachine.html#defer-class_method,
op参数是你代码中的&block,它在一个单独的线程中执行,另一方面callback参数是反应器触发的代码块一旦EM.defer返回结果。看看是否可以从 EM.defer 的回调块返回响应
标签: ruby rmagick eventmachine grape goliath