Rails从URL保存文件比直接Ruby更慢?

时间:2023-02-06 08:25:03

I have a Rails app that saves files in mongo. This works great and I have it set up to serve those files, but with some use cases I need to get the file and write it to disk (merging pdf files).

我有一个Rails应用程序,可以在mongo中保存文件。这很好用,我设置它来提供这些文件,但是在一些用例中我需要获取文件并将其写入磁盘(合并pdf文件)。

In IRB or from a simple Ruby file I can run the following code and get the file almost instantly, but when the same code is called from within Rails it times out.

在IRB或简单的Ruby文件中,我可以运行以下代码并几乎立即获取文件,但是当从Rails中调用相同的代码时,它会超时。

require 'open-uri'

open('id1_front.pdf', 'wb') do |file|
  file << open('http://127.0.0.1:3000/files/uploads/id1_front.pdf').read
  p file
end

-

Timeout::Error (Timeout::Error):
  app/controllers/design_controller.rb:38:in `block in save'
  app/controllers/design_controller.rb:37:in `save'

Anyone know why it would be timing out in Rails? Any alternate solutions to get a file out of mongo and write it to disk?

任何人都知道为什么它会在Rails中超时?从mongo获取文件并将其写入磁盘的任何替代解决方案?

thanks!

1 个解决方案

#1


3  

When you're running your development server, you have only one thread on which to respond to requests. This thread will be blocked when a request is being served: so, you request design_controller#save, which then tries to make another request to the web server for an uploaded file. This request will never successfully complete, because the webserver is still trying to complete the previous one.

当您运行开发服务器时,您只有一个线程可以响应请求。在提供请求时,将阻止此线程:因此,您请求design_controller#save,然后尝试向Web服务器发出另一个上载文件的请求。此请求永远不会成功完成,因为Web服务器仍在尝试完成上一个请求。

You might be able to get around this problem by using thin as your Rails server, instead of webrick. Add gem thin to your gemfile and start your server with rails s thin. I'm not sure if this will allow more than one request to be serviced simultaneously, but it's at least worth a shot.

您可以通过使用Thin作为Rails服务器而不是webrick来解决此问题。将gem thin添加到gemfile中,并使用rails s thin启动服务器。我不确定这是否允许同时为多个请求提供服务,但它至少值得一试。

--EDIT--

After some testing I determined that thin is also single-threaded, unfortunately, so will also have this exact same problem.

经过一些测试后,我确定瘦也是单线程的,不幸的是,所以也会有同样的问题。

After a bit of Googling, I did discover shotgun. It hasn't been active for awhile but it looks like it might fix your problem, since it spawns a new application per request in development. Give it a shot.

经过一段谷歌搜索,我确实发现了霰弹枪。它暂时没有活动,但看起来它可能会解决您的问题,因为它会在开发过程中为每个请求生成一个新的应用程序。试一试。

#1


3  

When you're running your development server, you have only one thread on which to respond to requests. This thread will be blocked when a request is being served: so, you request design_controller#save, which then tries to make another request to the web server for an uploaded file. This request will never successfully complete, because the webserver is still trying to complete the previous one.

当您运行开发服务器时,您只有一个线程可以响应请求。在提供请求时,将阻止此线程:因此,您请求design_controller#save,然后尝试向Web服务器发出另一个上载文件的请求。此请求永远不会成功完成,因为Web服务器仍在尝试完成上一个请求。

You might be able to get around this problem by using thin as your Rails server, instead of webrick. Add gem thin to your gemfile and start your server with rails s thin. I'm not sure if this will allow more than one request to be serviced simultaneously, but it's at least worth a shot.

您可以通过使用Thin作为Rails服务器而不是webrick来解决此问题。将gem thin添加到gemfile中,并使用rails s thin启动服务器。我不确定这是否允许同时为多个请求提供服务,但它至少值得一试。

--EDIT--

After some testing I determined that thin is also single-threaded, unfortunately, so will also have this exact same problem.

经过一些测试后,我确定瘦也是单线程的,不幸的是,所以也会有同样的问题。

After a bit of Googling, I did discover shotgun. It hasn't been active for awhile but it looks like it might fix your problem, since it spawns a new application per request in development. Give it a shot.

经过一段谷歌搜索,我确实发现了霰弹枪。它暂时没有活动,但看起来它可能会解决您的问题,因为它会在开发过程中为每个请求生成一个新的应用程序。试一试。