Created
March 29, 2011 16:35
-
-
Save hryk/892699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 受ける側 | |
| 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