Skip to content

Instantly share code, notes, and snippets.

@hryk
Created March 29, 2011 16:35
Show Gist options
  • Select an option

  • Save hryk/892699 to your computer and use it in GitHub Desktop.

Select an option

Save hryk/892699 to your computer and use it in GitHub Desktop.
# Gemfile
gem "ffi-rzmq"
# application.rb
module Foo
class Application
...
config.zeromq_context = ZMQ::Context.new
...
end
end
# コントローラにこんな感じのコードがある
def do_something
...
socket = Foo::Application.config.zmq_context.socket(ZMQ::REQ)
socket.connnect("tcp://172.0.0.1:3333")
# jobを投げる
socket.send_string("do something")
...
end
# 受ける側
require "em-zeromq"
class VarHandler
attr_reader :received
def on_readable(socket, messages)
messages.each do |m|
EM.defer do
# 何かする
...
socket.send_msg("finished")
end
end
end
end
EM.run do
ctx = EM::ZeroMQ::Context.new(1)
ctx.bind( ZMQ::REP, 'tcp://127.0.0.1:3333', VarHandler.new)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment