Created
July 29, 2010 17:15
-
-
Save hryk/498702 to your computer and use it in GitHub Desktop.
zmq pipeline sample
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
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'zmq' | |
| context = ZMQ::Context.new | |
| sock =context.socket(ZMQ::UPSTREAM) | |
| sock.bind("tcp://127.0.0.1:6000") | |
| loop do | |
| msg = sock.recv() | |
| puts msg | |
| 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
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'zmq' | |
| context = ZMQ::Context.new | |
| socket = context.socket(ZMQ::DOWNSTREAM) | |
| socket.bind("tcp://127.0.0.1:5000") | |
| loop do | |
| # send jobs to downstream worker | |
| puts "send job #{rand(100)}" | |
| msg = "Job number #{rand(100)}" | |
| socket.send(msg) | |
| sleep 1 | |
| 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
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'zmq' | |
| worker_num = ARGV[0] || '1' | |
| context = ZMQ::Context.new | |
| socket_up = context.socket(ZMQ::UPSTREAM) | |
| socket_down = context.socket(ZMQ::DOWNSTREAM) | |
| socket_up.connect("tcp://127.0.0.1:5000") | |
| socket_down.connect("tcp://127.0.0.1:6000") | |
| puts "Worker #{worker_num} start." | |
| loop do | |
| # send jobs to downstream worker | |
| msg = socket_up.recv(); | |
| puts msg | |
| down_msg = "[worker ##{worker_num}] #{msg}" | |
| socket_down.send(down_msg); | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a supremely useful gist for learning zmq! I was looking for just this. One thing I noticed though is that collector directly sends to all the workers, and if it idsn't running, you can't add workers. If you do, they don't et anything...