README.md in ezmq-0.2.0 vs README.md in ezmq-0.3.0

- old
+ new

@@ -45,10 +45,11 @@ server = EZMQ::Server.new server.listen do |message| puts message 'Thanks for the message!' # The return of the block is sent to the client. end +``` JSON Echo Server ---------------- Waits for JSON message, decodes it, re-encodes it, and sends it back. @@ -96,9 +97,67 @@ require 'ezmq' subscriber = EZMQ.Subscriber.new topic: 'foorever' subscriber.listen ```` + +Pipeline Work Generator +------------------------ +Generates work, distributes it to workers via PUSH socket. + +``` +require 'ezmq' +require 'json' + +generator = EZMQ::Pusher.new :bind, encode: -> m { JSON.dump m } + +15.times do |id| + work = { 'id' => "task_#{ id }", 'request' => '100' } + puts "Generated work #{work}" + generator.send work +end + +``` + +Pipeline Workers +--------------- +3 worker threads PULL work from the Generator and PUSH results to the Collector. + +The 'work' here is generating a random number between 1 and a requested maximum. + +``` +require 'ezmq' +require 'json' + +workers = [] + +3.times do |id| + workers << Thread.new do + input = EZMQ::Puller.new :connect, decode: -> m { JSON.load m } + output = EZMQ::Pusher.new port: 5556, encode: -> m { JSON.dump m } + input.listen do |work| + puts "Worker #{id} pulled #{work}" + result = rand(1..work['request'].to_i) + report = { 'id' => work['id'], 'result' => result } + output.send report + end + end +end + +workers.each(&:join) +``` + +Pipeline Results Collector +-------------------------- +PULLs results from workers and prints it to STDOUT. + +``` +require 'ezmq' +require 'json' + +collector = EZMQ::Puller.new port: 5556 +collector.listen +``` Operating System Notes ====================== As this relies on [ffi-rzmq](https://github.com/chuckremes/ffi-rzmq), you will need to have the zeromq libraries available.