Skip to content

Instantly share code, notes, and snippets.

@niamster
Created March 14, 2015 23:59
Show Gist options
  • Save niamster/40c98c0fd196c6b0d458 to your computer and use it in GitHub Desktop.
Save niamster/40c98c0fd196c6b0d458 to your computer and use it in GitHub Desktop.
Celluloid 0MQ dealer
require 'celluloid/zmq'
Celluloid::ZMQ.init
class Client
include Celluloid::ZMQ
def initialize(address)
@socket = DealerSocket.new
@socket.set ::ZMQ::SNDHWM, 0
@socket.set ::ZMQ::RCVHWM, 0
begin
@socket.connect(address)
rescue IOError
@socket.close
raise
end
end
def run
loop do
async.handle_response(@socket.read_multipart)
end
end
def write(*message)
@socket.send(*message)
nil
end
def handle_response(msg)
puts "Received: #{msg}"
end
end
addr = 'tcp://127.0.0.1:3435'
clients = Client.new(addr)
clients.async.run
# Fire loads of non-blocking requests at the same time over the same socket
futures = []
10000.times do |i|
futures << clients.future.write("#{i}", "Test #{i}")
end
futures.reduce(0) {|sum, f|}
sleep 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment