Skip to content

Instantly share code, notes, and snippets.

@bcg
Created April 6, 2011 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcg/906489 to your computer and use it in GitHub Desktop.
Save bcg/906489 to your computer and use it in GitHub Desktop.
Eventmachine + ZMQ + Redis Server
require 'em-zeromq'
Thread.abort_on_exception = true
EM.run do
ctx = EM::ZeroMQ::Context.new(1)
socket = ctx.connect( ZMQ::PUSH, 'tcp://127.0.0.1:15000')
loop do # ZOMG you are blocking!?
socket.send_msg("test")
end
end
require 'em-zeromq'
require 'em-redis'
Thread.abort_on_exception = true
$count = 0
$time = nil
class MessageHandler
attr_reader :received
def on_readable(socket, messages)
messages.each do |m|
$count+=1
$time = Time.now if $count == 1
$redis.lpush('q', m)
#puts m.copy_out_string
if (($count % 10000) == 0)
puts "T: #{Time.now-$time}"
$time = Time.now
end
end
end
end
EM.run do
$redis = EM::Protocols::Redis.connect
ctx = EM::ZeroMQ::Context.new(1)
socket = ctx.bind( ZMQ::PULL, 'tcp://127.0.0.1:15000', MessageHandler.new)
puts "Server up and waiting..."
end
# T: 0.792741
# T: 0.830816
# T: 0.798252
# T: 0.740848
# T: 0.927369
# T: 0.759275
# T: 0.863073
# T: 0.966802
# T: 0.812553
# T: 0.818634
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment