Last active
August 29, 2015 13:57
-
-
Save threadhead/9491295 to your computer and use it in GitHub Desktop.
Celluloid IO Looper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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