Skip to content

Instantly share code, notes, and snippets.

@threadhead
Last active August 29, 2015 13:57
Show Gist options
  • Save threadhead/9491295 to your computer and use it in GitHub Desktop.
Save threadhead/9491295 to your computer and use it in GitHub Desktop.
Celluloid IO Looper
require 'celluloid'
require 'celluloid/io'
class MyPoll
include Celluloid::IO
finalizer :shutdown
def initialize
@socket = TCPSocket.open('192.168.0.55', 2222)
end
def reader
r = @socket.read
puts "socket read: #{r.inspect}"
end
def shutdown
@socket.close if @socket
end
end
class MyLooper
include Celluloid
def run
@socket = MyPoll.new
loop do
@socket.reader
puts 'doing other stuff...'
end
end
def term
@socket.terminate
end
end
sup = MyLooper.new
trap("INT") { sup.term; exit }
sup.async.run
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment