Sha256: 939457ba66a815a0daf21c8c49536863d2308f4db33efbc03c98c91addb1e254

Contents?: true

Size: 952 Bytes

Versions: 26

Compression:

Stored size: 952 Bytes

Contents

$:.unshift File.dirname(__FILE__) + '/../lib'
require 'mq'

MAX = 500

def log *args
  p args
end

# MQ.logging = true

EM.run{

  # worker

  log "prime checker", Process.pid, :started

  class Fixnum
    def prime?
      ('1' * self) !~ /^1?$|^(11+?)\1+$/
    end
  end

  MQ.queue('prime checker').subscribe{ |info, num|
    EM.defer(proc{

      log "prime checker #{Process.pid}-#{Thread.current.object_id}", :prime?, num
      if Integer(num).prime?
        MQ.queue(info.reply_to).publish(num, :reply_to => "#{Process.pid}-#{Thread.current.object_id}")
        EM.stop_event_loop if num == '499'
      end
      
    })
  }

  # controller

  MQ.queue('prime collector').subscribe{ |info, prime|
    log 'prime collector', :received, prime, :from, info.reply_to
    (@primes ||= []) << Integer(prime)
  }

  MAX.times do |i|
    EM.next_tick do
      MQ.queue('prime checker').publish((i+1).to_s, :reply_to => 'prime collector')
    end
  end

}

Version data entries

26 entries across 26 versions & 8 rubygems

Version Path
adamh-amqp-0.6.3.1 research/primes-threaded.rb
brontes3d-amqp-0.6.4.0 research/primes-threaded.rb
brontes3d-amqp-0.6.4.1 research/primes-threaded.rb
brontes3d-amqp-0.6.4.2 research/primes-threaded.rb
brontes3d-amqp-0.6.4.3 research/primes-threaded.rb
brontes3d-amqp-0.6.4 research/primes-threaded.rb
fotonauts-amqp-0.6.1 research/primes-threaded.rb
fotonauts-amqp-0.6.4 research/primes-threaded.rb
tmm1-amqp-0.5.1 examples/primes-threaded.rb
tmm1-amqp-0.6.1 research/primes-threaded.rb
tmm1-amqp-0.6.3 research/primes-threaded.rb
tmm1-amqp-0.6.4 research/primes-threaded.rb
ivanvanderbyl-amqp-0.6.13.1 research/primes-threaded.rb
amqp-0.7.0.pre research/primes-threaded.rb
totty-amqp-0.6.7.1 research/primes-threaded.rb
totty-amqp-0.6.7.1.totty research/primes-threaded.rb
arvicco-amqp-0.6.13 research/primes-threaded.rb
arvicco-amqp-0.6.11 research/primes-threaded.rb
arvicco-amqp-0.6.10 research/primes-threaded.rb
arvicco-amqp-0.6.9 research/primes-threaded.rb