From: Francis Cianfrocca Date: 2006-06-07T01:47:27+09:00 Subject: Re: Multiple threads on single port ------=_Part_18121_26003256.1149612442009 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline It works, but not terribly well. Whenever you call TCPServer#accept in a program with more than one thread, the acceptor socket goes on a wait queue inside Ruby until it's readable- it doesn't cross into the kernel. But I tried your program and stress-tested it with ten threads, and performance was abysmal. So I'd guess there's some interaction with Ruby's thread scheduler that isn't quite normal. I'm not sure what you're trying to do but you might be better off calling accept in one thread and passing the accepted sockets to a thread pool, or using nonblocking IO. Another standard alternative is to fork a process after accepting each socket- this is a perfectly good solution if your performance needs are low. On 6/6/06, Stuart Brand wrote: > > Hi all > > Can I use Ruby threads to have more then 1 server process running on the > same port > > something like > > require 'socket' > port = (ARGV[0] || 80).to_i > server = TCPServer.new('localhost', port) > threads = [] > > > 10.times do |i| > threads[i] = Thread.new { > while (session = server.accept) > puts "Request: #{session.gets}" > session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n" > session.print "

#{Time.now}

\r\n" > session.close > end > } > end > > Would this produce 10 processes that could take independent connection? > > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_18121_26003256.1149612442009--