From: Mikael Brockman Date: 2004-11-26T22:17:03+09:00 Subject: [ANN] Multiplexer - linear non-blocking I/O Blocking I/O is really easy to use. But when you use it to write servers, you run into problems: you can't run two blocking syscalls simultaneously. So if you're writing a huge file to some guy, every other client is stalled, and no one new can connect. Unacceptable, for many types of servers. They need non-blocking I/O. Non-blocking I/O is a lot more annoying to use. Instead of going | write "Hello. What's your name?" | name = read_line | write "How do you do, #{name}?" | state = read_line | write "It's nice to hear that you're #{state}, #{name}." we have to make weird state machines. The good news: we can use callcc to make the non-blocking nature practically invisible. Multiplexer does that. Here's how you'd write a hello server: | class Test < Multiplexer::Handler | def handle | write_line "Hello. What's your name?" | name = read_line | write_line "How do you do, #{name}?" | state = read_line | write_line "It's nice to hear that you're #{state}, #{name}." | disconnect | end | end | | def test | multiplexer = Multiplexer::Multiplexer.new 0.5 | multiplexer.listen 31337, Test | multiplexer.run | end | | test It'll run in one thread. Multiplexer handles the select(3) calls. Get it: http://www.phubuh.org/Projects/nntpu/_darcs/current/multiplexer.rb