Rails does not block on file uploads 5

Posted by pratik
on Saturday, September 13

I’ve been quite annoyed with the ignorance of people who keep telling every other person that Rails/Mongrel blocks on file upload.

Here are the steps to realize Rails does not block on file uploads :

  • Follow these steps :
1
2
3
null:Rails lifo$ rails fileuploads
null:Rails lifo$ cd fileuploads/
null:fileuploads lifo$ script/generate controller upload index

Make your RAILS_ROOT/app/views/upload/index.html.erb look something like below :

1
2
3
4
5
<% form_tag({}, { :multipart => true }) do %>
  <label for="file">File to Upload</label>
  <%= file_field_tag "file" %>
  <%= submit_tag %>
<% end %>

Now, generate a big fat file ( YMMV ) for testing purpose :


null:fileuploads lifo$ dd if=/dev/random of=out.txt bs=1000000 count=100
  1. Now start the server with script/server
  2. Open http://localhost:3000/upload in two tabs
  3. Start uploading that big fat file in first tab
  4. Switch the tab to second. And start pressing refresh button !!!

In all fairness, you may notice a momentary spike in memory/cpu usage while dealing with very large file uploads ( we’re talking a good 100s of MB here ) But believing that Rails process is blocked for the entire time it takes a user to upload the file from the browser, is outrageous.

Comments

Leave a response

  1. crayzSeptember 13, 2008 @ 07:15 PM

    Actually I think you’re wrong. The default script/server, if you have it installed, will be mongrel. I don’t have a link handy, but I can recall Zed talking about coding mongrel to handle file uploads outside of Rails (and then hand the uploaded file to Rails once it’s complete)

    Try ‘script/server webrick’ and the file upload does indeed block other requests. So from a practical perspective you may be right, but technically not – if the upload has to be handled by Rails, it will block other requests

  2. PratikSeptember 13, 2008 @ 07:30 PM

    I really meant to say Rails/Mongrel ( edited in the post now ). I have nfi about webrick.

    Thanks!

  3. Hongli LaiSeptember 14, 2008 @ 10:28 AM

    crayz: Nobody uses webrick in production so that isn’t a problem.

  4. Ken CollinsSeptember 14, 2008 @ 04:14 PM

    I personally liked The Book Of Moron that I saw cached in my feed reader. Thanks for the post!!!!

  5. Mike PerhamOctober 09, 2008 @ 10:28 PM

    Thanks for dispelling this, Pratik. Ignoring the question as to whether or not they blocked, my testing concluded that Merb was significantly faster than Rails in handling file uploads a year ago. It would be interesting to compare the two today, with all the work that has been done to remove thread synchronization and speed up routing in the last year.

    http://www.mikeperham.com/2007/10/02/file-uploads-in-merb-versus-rails/

Comment