# Png-file as a HTTP-Response

**URL:** <https://www.ruby-forum.com/t/png-file-as-a-http-response/180553>\
**Category:** Ruby\
**Created:** [December 21, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/png-file-as-a-http-response/180553 "2009-12-21T18:21:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [December 21, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/png-file-as-a-http-response/180553/1 "2009-12-21T18:21:05Z")

</div>

Hello,

Im quite stuck with my little project of writting a File-Controller in  
ruby.  
The actual problem is, that if I stream a png-file to a client  
TCP-Socket, the browser is getting the file realy slowly, so that it is  
possible to see the file beeing build on the page. It doesn’t matter if  
I do this on a 100MbBits/s Server or doing it as localhost. I tried many  
diffrent ways of writing to the client-socket: syswrite, puts, write and  
all with 1024 buffer or without any buffer. The result is just the same,  
a very slow answer.  
I am not a ruby-expert and probably it is just a misstake of my own, but  
I hope you will help me with this, because I am realy stuck…

thanks…

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [December 24, 2009, 6:17am UTC](https://www.ruby-forum.com/t/png-file-as-a-http-response/180553/2 "2009-12-24T06:17:07Z")

</div>

Robert Murmel wrote:

> the browser is getting the file realy slowly  
> […]
> 
> server = Thread.start {  
> while (cl = serverSocket.accept)  
> […]  
> end  
> }  
> while (true)  
> true  
> end

This “while true; end” is known as a “busy loop” (google for this term)  
and is not CPU-friendly. Perhaps it steals CPU cycles form your working  
thread.

Remove this busy loop and the “server = Thread.start” line and have only  
this:

while (cl = serverSocket.accept)  
[…]  
end

and see if it solves the problem.

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [December 25, 2009, 9:58am UTC](https://www.ruby-forum.com/t/png-file-as-a-http-response/180553/3 "2009-12-25T09:58:59Z")

</div>

Albert S. wrote:

> > server = Thread.start {  
> > while (cl = serverSocket.accept)  
> > […]  
> > end  
> > }  
> > while (true)  
> > true  
> > end
> 
> This “while true; end” is known as a “busy loop” (google for this term)  
> and is not CPU-friendly. Perhaps it steals CPU cycles form your working  
> thread.

you can just replace it with:

server.join
