The ruby way of interrupting a Kernel::select?


#1

Hello everyone,

As a newbie with too much java-experience, I found it strange that I
couldn’t interrupt the Kernel::select.

Basically what I wanted to do would be to interrupt the select as
soon as a socket decided it wanted to write something, and then add
that channel to the write-interest array.

I found no obvious way of doing that, so I made a hack around it: I
registered a pipe with the read-interest and every time I wanted to
write something then I would send a single byte on the pipe, which
would interrupt the select and allow me to register the socket with
the array.

I.e. something like this:

8<-------------

@notifier, @requester = IO.pipe # this is my hack
	puts "Server started on port #{port} with handler #{@delegate}."
	while (true)
		@read = [@server, @notifier]
		@write = Array.new
		@error = [@server]
		@connections.each do |connection|
			@read << connection.io if connection.read
			@write << connection.io if connection.write
			@error << connection.io if connection.read
		end
		read, write, error = Kernel::select(@read, @write, @error)
		@connections.each do |connection|
			connection.handle_read if read.include? connection.io
			connection.handle_write if write.include? connection.io
			connection.handle_error if error.include? connection.io
		end
		@notifier.getc if read.include? @notifier
		handle_connect if read.include? @server
	end

8<------------------

def poke
	#this is a hack to interrupt Kernel::select
	puts "Poking!"
	@requester.putc "!"
end

8<------------------

Please tell me there’s a better way! :slight_smile:

/Christoffer