Skip to content

Instantly share code, notes, and snippets.

@cpuguy83 cpuguy83/client.rb
Last active Dec 30, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.