I realize this is a Ruby mailing list but I have a Ruby/Rails question
that
seems to fall more on the Ruby side of the fence. I have a library file
that creates a named pipe and writes the results of ping to the pipe.
This
method is called via a get AJAX request to kick off the process.
def self.run(address)
Fifo.open(PINGFIFO)
Thread.new {
IO.popen "ping -c 10 localhost" do |f|
until f.eof?
Fifo.write(f.gets, PINGFIFO)
end
end
}
end
Then in a 2nd AJAX request I simply attempt to read the named pipe and
return the string to the browser:
def results
if File.exists?(PINGFIFO) && File.pipe?(PINGFIFO)
File.open(PINGFIFO, “r”) do |f|
render :text => f.readlines
end
end
end
The problem is the 2nd AJAX request simply hangs indefinitely. If I
tail
the pipe I can see data being written to it. Does anyone have any
insight
into why this is happening? It seemed like a simple enough problem but
this
one is throwing me for a loop (har!).
The semantics of pipes are somewhat tricky and definitely not ideal for
this
sort of communication.
I’m not exactly sure what your use case is, but you might consider
placing
your message into a service like Redis. Redis 2.0 (still in RC right
now)
supports “blocking pop”, which means your “writer” simply adds a message
to
a list, then your “reader” attempts a blocking pop. If a message is
available the reader will get it immediately, otherwise it blocks until
a
message is written to the list it’s trying to pop from.
On May 29, 2010, at 1:39 PM, Zundra D. wrote:
until f.eof?
Fifo.write(f.gets, PINGFIFO)
end
end
}
end
The problem is the 2nd AJAX request simply hangs indefinitely. If I tail
the pipe I can see data being written to it. Does anyone have any insight
into why this is happening? It seemed like a simple enough problem but this
one is throwing me for a loop (har!).
My first thought is buffering. I’m not sure what the semantics of
Fifo.open and Fifo.write are in your code since ‘Fifo’ isn’t a standard
Ruby class. It also seems strange that you are calling Fifo.open but not
capturing a return value to be used anywhere. Perhaps you need to call
flush somewhere after Fifo.write to ensure the data is actually written
to the fifo? That might explain why your send request is blocking since
the data isn’t in the fifo yet.
Gary W.
The only value Fifo.open returns is the name of the pipe. Since the
pipe’s
name is known (stored in a constant) I don’t need to capture the return
value. Flush is being called in the write method
---------- Forwarded message ----------
From: Gary W. [email protected]
Date: Sat, May 29, 2010 at 2:37 PM
Subject: Re: Non blocking IO during an AJAX request
To: ruby-talk ML [email protected]
On May 29, 2010, at 1:39 PM, Zundra D. wrote:
I realize this is a Ruby mailing list but I have a Ruby/Rails question
that
seems to fall more on the Ruby side of the fence. I have a library file
that creates a named pipe and writes the results of ping to the pipe.
This
end
}
end
The problem is the 2nd AJAX request simply hangs indefinitely. If I tail
the pipe I can see data being written to it. Does anyone have any insight
into why this is happening? It seemed like a simple enough problem but
this
one is throwing me for a loop (har!).
My first thought is buffering. I’m not sure what the semantics of
Fifo.open
and Fifo.write are in your code since ‘Fifo’ isn’t a standard Ruby
class. It
also seems strange that you are calling Fifo.open but not capturing a
return
value to be used anywhere. Perhaps you need to call flush somewhere
after
Fifo.write to ensure the data is actually written to the fifo? That
might
explain why your send request is blocking since the data isn’t in the
fifo
yet.
Gary W.