Non blocking IO during an AJAX request

Z

Zundra Daniel

[Note: parts of this message were removed to make it a legal post.]

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!).
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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.
 
G

Gary Wright

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.
=20
def self.run(address)
Fifo.open(PINGFIFO)
=20
Thread.new {
IO.popen "ping -c 10 localhost" do |f|
until f.eof?
Fifo.write(f.gets, PINGFIFO)
end
end
}
end
=20
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 Wright=
 
Z

Zundra Daniel

[Note: parts of this message were removed to make it a legal post.]

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 Wright <[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]>



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

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 Wright
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top