From: Marcelo Date: 2008-03-27T23:14:40+09:00 Subject: Re: "loop do IO.select([io], nil, nil)" eats 95% of CPU, any other way? On Thu, Mar 27, 2008 at 4:47 AM, Iñaki Baz Castillo wrote: > I've tryed now with TCPServer alone (and Threads) without GServer and > this loop issue doesn't occur: > > --- io3.rb ------------------------------------ > #!/usr/bin/env ruby > > require 'socket' > > server = TCPServer.open(2000) > > loop do > p "------- main loop --------" > socket = server.accept accept blocks if there's no connection available, unless you tell the system not to. The default is to block. > So I don't know why but using GServer the main loop runs all the time > while witout using GServer it doesn't occur ¿? Because... > > def serve(io) > > > > puts "------------ serve(io) -------------" > > loop do > > p "-- second loop --" > > > > ready, = select([io], nil, nil, nil).first This will block if there's no data available but the file descriptor hasn't reached EOF. > > loop do > > #puts "----- main loop -----" > > break if server.stopped? > > end This code doesn't block, so it will loop as fast as it can. Try replacing it with something like: loop do puts "----- main loop -----" sleep(1) break if server.stopped? end Note that GServer.start will spawn a new thread, which handles all the incoming connections. It will spawn a new thread for each new connection, so your "serve" method has to handle it and then exit, it doesn't make sense for it to keep lingering around after the client has gone away. You can also replace your main loop by server.join. Marcelo