K
Ken
As far as I can tell, YAML.load will not return until the sender closes
the socket. Is this true? If true, is this desirable?
the socket. Is this true? If true, is this desirable?
Ken said:As far as I can tell, YAML.load will not return until the sender closes
the socket. Is this true? If true, is this desirable?
If you need to have YAML.load return before you've closed the socket,
send a pause indicator "\n...\n". YAML.load will return and you can
read further documents from the same IO.
_why
Ken said:I tried sending the pause indicator using a variety of plausible IO
methods (write, print, puts, etc.) but without success. The receiver
listed below prints the data but only after five seconds.
Ken said:I tried sending the pause indicator using a variety of plausible IO
methods (write, print, puts, etc.) but without success. The receiver
listed below prints the data but only after five seconds.
The YAML loader tries to read enough to fill its buffer. It might be
better to use IO#readline. The only way to do this for now is to
overload IO#read so it will read a line at a time.
Here's a replacement receiver:
require "socket"
require "yaml"
socket = TCPSocket.new("localhost", 5555)
class << socket
alias _read read
def read( len )
readline
end
end
data = YAML.load(socket)
p data
Also, the parser isn't stopping at the pause indicator properly and is
requiring an additional newline.
Here's a replacement sender:
data = { 'a' => [1,2,3], 'b' => 43 }
server = TCPServer.new("localhost", 5555)
socket = server.accept
YAML.dump(data, socket)
socket.write "\n...\n\n"
sleep 5
Stream parsing like this is totally untested. Time to remedy the
situation.
_why
Ken said:As a Ruby noob, it's a beautiful thing to see that major contributors to
the language are accessible and eager to help. Thanks again.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.