# Queue/IO blocking in threads

**URL:** <https://www.ruby-forum.com/t/queue-io-blocking-in-threads/161911>\
**Category:** Ruby\
**Created:** [March 3, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/queue-io-blocking-in-threads/161911 "2009-03-03T19:52:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [March 3, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/queue-io-blocking-in-threads/161911/1 "2009-03-03T19:52:24Z")

</div>

I am coding a client/server in which requests and responses are handled  
on the client and server side by threads communicating with Queues  
(because several sessions will share one connection to the server).

In the code below, I expect the client and server to exchange the same  
message indefinitely, but between reading the queues and IO, it blocks  
somewhere. it doesn’t get further than printing the first request.

require ‘gserver’  
require ‘socket’  
require ‘zlib’  
require ‘yaml’

class ConnectionHandler  
def initialize(io)  
queue = Queue.new  
request\_handler = Thread.new do  
loop do  
req = io.gets(“ **EOF** ”).chomp(“ **EOF** ”)  
puts “client request: #{req}”  
queue \<\< “speak your request, my child”  
end  
end

```
response_handler = Thread.new do
  loop do
    res = queue.pop
    socket << res << " __EOF__"
  end
end

request_handler.join
response_handler.join

```

end  
end

class MyServer \< GServer  
def initialize(port=10001, \*args)  
super(port, \*args)  
end  
def serve(io)  
ConnectionHandler.new(io)  
end  
end

# Start the server

server = MyServer.new  
server.audit = true  
server.start

# Here follows the client implementation:

socket = TCPSocket.new(‘localhost’, 10001)  
cqueue = Queue.new  
request\_processor = Thread.new {  
loop do  
req = cqueue.pop  
socket \<\< req \<\< “ **EOF** ”  
end  
}

response\_processor = Thread.new {  
loop do  
res = socket.gets(“ **EOF** ”).chomp(“ **EOF** ”)  
puts “server response: #{res}”  
cqueue \<\< “enquiring minds want to know”  
end  
}

# This message will start off the conversation

cqueue \<\< “can i speak freely?”

server.join  
request\_processor.join  
response\_processor.join

---

<div class="post-metadata">

**Author:** ![mjcharl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mjcharl](https://www.ruby-forum.com/u/mjcharl)\
**Post date:** [March 3, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/queue-io-blocking-in-threads/161911/2 "2009-03-03T20:15:05Z")

</div>

I am very sorry; I just realized that in the response\_handler thread I  
was writing to “socket” instead of “io”. I was actually just trying to  
present a simplification of the problem I am having with my real  
project. Arrgh! Now to go sift through all the lines of code to see if I  
can get the real thing to work…
