question about YAML over sockets

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?
 
W

why the lucky stiff

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
 
K

Ken Collins

As far as I can tell, YAML.load will not return until the sender closes
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

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.

Sender:

require "socket"
require "yaml"

data = { 'a' => [1,2,3], 'b' => 43 }
server = TCPServer.new("localhost", 5555)
socket = server.accept
YAML.dump(data, socket)
socket.write "\n...\n"
sleep 5

Receiver:

require "socket"
require "yaml"

socket = TCPSocket.new("localhost", 5555)
data = YAML.load(socket)
p data
 
W

why the lucky stiff

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
 
K

Ken Collins

Thanks, _why, that change works for me.

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.


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
 
W

why the lucky stiff

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.

Likewise, it's good to see growth. And openness to fledgling
technology. And you did get a bit lucky, I can be so ignorant and
aloof sometimes. Well, stick around.

_why
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top