Forum: Ruby The ruby way of interrupting a Kernel::select?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E4d8c6e0e9e5ca6ab51b4b1937586e6f?d=identicon&s=25 Christoffer Lernö (Guest)
on 2006-02-11 23:37
(Received via mailing list)
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! :)


/Christoffer
This topic is locked and can not be replied to.