Queue/IO blocking in threads

Discussion in 'Ruby' started by Martin Ceronio, Mar 3, 2009.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
    Martin Ceronio, Mar 3, 2009
    #1
    1. Advertising

  2. 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...
    --
    Posted via http://www.ruby-forum.com/.
    Martin Ceronio, Mar 3, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    12,534
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    926
    Valentin Tihomirov
    Oct 12, 2003
  3. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    679
    Russell Warren
    Jun 27, 2006
  4. Frank Millman
    Replies:
    3
    Views:
    344
    Frank Millman
    Feb 9, 2010
  5. Kris
    Replies:
    0
    Views:
    479
Loading...

Share This Page