Skip to content

Instantly share code, notes, and snippets.

@RyanScottLewis
Created November 7, 2014 16:21
Show Gist options
  • Select an option

  • Save RyanScottLewis/8de634eadae415d87364 to your computer and use it in GitHub Desktop.

Select an option

Save RyanScottLewis/8de634eadae415d87364 to your computer and use it in GitHub Desktop.
PubSub::Session#initialize(publisher_thread_limit=5, subscriber_thread_limit=5)
PubSub::Session#queue
PubSub::Session#publish(message) # Pull a thread from the publisher thread pool and in it, create a new Publisher instance and then push to the queue with it
PubSub::Session#wait # # Pull a thread from the subscriber thread pool and in it, create a new Subscriber instance and then pop from the queue with it, then perform the subscribers #on_message
PubSub::Session#publishers
PubSub::Session#subscribers
PubSub::Publisher#initialize(session)
PubSub::Publisher#session
PubSub::Publisher#publish(message)
PubSub::Subscriber#initialize(session)
PubSub::Subscriber#session
PubSub::Subscriber#on_message(message)
PubSub::Subscriber#run
class MySubscriber < PubSub::Subscriber
def on_message(message)
puts "Got #{message}"
end
end
session = PubSub::Session.new
5.times { session.subscribers << MySubscriber.new }
threads = []
50.times do |i|
Thread.new { session.publish( current_number: i ) }
end
threads.each(&:join)
puts "All publishers finished!"
session.wait
puts "All subscribers finished!"
# OR: Do them both at the same time for super impressiveness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment