Ruby Lock IO.read ()?

I have a webservice method that reads a photo and returns its byte data. I am currently doing the following:

@photo_bytes = IO.read("/path/to/file")
send_data(@photo_bytes, :filename => "filename", :type => "filetype", :disposition => "inline")

I get weird behavior when calling this a lot ... sometimes send_data returns null. I think that maybe I get approval if the file is not closed yet. Do I need to explicitly close the file after opening it with IO.read? How could I use read_nonblock for this, and was it worth it?

UPDATE:

So, I made some entries, and sometimes IO.read returns a value of 1800 bytes, when it usually returns ~ 5800 bytes for an image. When it returns 1800 bytes, the image is not displayed on the client. This happens in a rather random way when two users call a web service.

thanks

Tom

+3
1

IO.read . , , , , . write (not append) , , , , .

, * NIX, Linux OS X, :

require 'tempfile'
require 'fileutils'

def safe_write(path, data)
  tmp = Tempfile.new
  tmp.write(data)
  tmp.close
  FileUtils.mv(tmp.path, path)
end

, "/path/to/file" , .

+4

Source: https://habr.com/ru/post/1716537/


All Articles