# Non blocking IO during an AJAX request

**URL:** <https://www.ruby-forum.com/t/non-blocking-io-during-an-ajax-request/189363>\
**Category:** Ruby\
**Created:** [May 29, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/non-blocking-io-during-an-ajax-request/189363 "2010-05-29T19:39:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zundra\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@Zundra\_D](https://www.ruby-forum.com/u/Zundra_D)\
**Post date:** [May 29, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/non-blocking-io-during-an-ajax-request/189363/1 "2010-05-29T19:39:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zundra\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@Zundra\_D](https://www.ruby-forum.com/u/Zundra_D)\
**Post date:** [May 29, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/non-blocking-io-during-an-ajax-request/189363/2 "2010-05-29T20:00:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zundra\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@Zundra\_D](https://www.ruby-forum.com/u/Zundra_D)\
**Post date:** [May 29, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/non-blocking-io-during-an-ajax-request/189363/3 "2010-05-29T20:38:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zundra\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@Zundra\_D](https://www.ruby-forum.com/u/Zundra_D)\
**Post date:** [May 29, 2010, 9:38pm UTC](https://www.ruby-forum.com/t/non-blocking-io-during-an-ajax-request/189363/4 "2010-05-29T21:38:26Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Sat, May 29, 2010 at 2:37 PM  
Subject: Re: Non blocking IO during an AJAX request  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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.
