Skip to content

Instantly share code, notes, and snippets.

@cpuguy83
Last active December 30, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpuguy83/7852579 to your computer and use it in GitHub Desktop.
Save cpuguy83/7852579 to your computer and use it in GitHub Desktop.
Crude demo of async I/O using Celluloid::IO Actor with external class utilizing Celluoid::IO::TCPSocket. No need for GIL-less Ruby for this to work (except perhaps on the server, which isn't using evented IO for this demo)
require 'celluloid'
require 'celluloid/io'
class MySocket
def go
@sock ||= Celluloid::IO::TCPSocket.open('localhost', 2000)
puts "#{@sock.read}"
end
end
class MyProcessor
include Celluloid::IO
def run
@sock ||= MySocket.new
"#{@sock.go} #{@sock.go}"
end
end
worker = MyProcessor.new
2.times {
worker.future.run
}
require 'socket'
server = TCPServer.new(2000)
i = 0
loop do
client = server.accept
i += 1
puts "Client connected!"
Thread.new do
client_num = i
client.puts "Hi client #{client_num}!"
if i > 1
sleep 5
else
sleep 30
end
client.puts "Bye client #{client_num}!"
client.close
puts "Closed connection #{client_num}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment