Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Forked from dlecocq/queues.rb
Last active January 4, 2016 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/8550092 to your computer and use it in GitHub Desktop.
Save myronmarston/8550092 to your computer and use it in GitHub Desktop.
➜ 8549841 git:(master) ✗ ruby queues.rb
Popping before put
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: []
pushed 1
First pop after put...
popped job:
popped job:
popped job:
popped job:
popped job:
popped job: 1
yielding: 1
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: [1]
Second pop after put...
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: []
➜ 8549841 git:(master) ✗ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
➜ 8549841 git:(master) ✗ chruby jruby ➜ 8549841 git:(master) ✗ ruby queues.rb
Popping before put
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: []
pushed 1
First pop after put...
popped: []
popped job:
Second pop after put... popped job:
popped job:
popped job:
popped job:
popped job: 1
yielding: 1
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: [1]
➜ 8549841 git:(master) ✗ gitx
➜ 8549841 git:(master) ✗
➜ 8549841 git:(master) git remote add myron https://gist.github.com/8550092.git
➜ 8549841 git:(master) git push myron
fatal: You are pushing to remote 'myron', which is not the upstream of
your current branch 'master', without telling me what to push
to update which remote branch.
➜ 8549841 git:(master) git push myron master
Username for 'https://gist.github.com': myronmarston
Password for 'https://myronmarston@gist.github.com':
fatal: Authentication failed
➜ 8549841 git:(master) cat queues.rb| pbcopy
➜ 8549841 git:(master) chruby 1.9.3
➜ 8549841 git:(master) ruby queues.rb
Popping before put
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: []
pushed 1
First pop after put...
popped job:
popped job:
popped job:
popped job:
popped job:
popped job: 1
yielding: 1
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: [1]
Second pop after put...
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: []
➜ 8549841 git:(master) chruby jruby
ruby qu % ➜ 8549841 git:(master) ruby queues.rb
Popping before put
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped: []
pushed 1
First pop after put...
popped: []
Second pop after put...
popped job:
popped job:
popped job:
popped job:
popped job:
popped job: 1
yielding: 1
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
popped job:
popped: [1]
popped job:
popped job: popped job: popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
popped job:
yielding nil
#! /usr/bin/env ruby
@queues = 10.times.map { Array.new }
# An enumerator that yields `nil` if none of the queues has any items, but
# otherwise round-robins across them.
@foo = Enumerator.new do |enum|
loop do
found = false
@queues.each do |queue|
job = queue.pop
puts " popped job: #{job}"
unless job.nil?
found = true
puts " yielding: #{job}"
enum.yield(job)
end
end
puts " yielding nil"
enum.yield(nil) unless found
end
end
# Pop up to 'count' objects from
def pop(count)
jobs = []
while jobs.length < count
job = @foo.next
break if job.nil?
jobs.push(job)
end
jobs
end
# First, verify that the there are no items available
puts "Popping before put"
puts "popped: #{pop(5)}"
@queues[5].push(1)
puts "pushed 1"
# Now, I'd expect the item to be immediately available after putting it, but in
# jRuby, it's not yielded until the second pop.
puts "First pop after put..."
puts "popped: #{pop(5)}"
puts "Second pop after put..."
puts "popped: #{pop(5)}"
# jruby 1.7.9 (1.9.3p392) 2013-12-06 87b108a on Java HotSpot(TM) 64-Bit Server VM 1.7.0_45-b18 [darwin-x86_64]
# Pop before put: []
# First pop after put: []
# Second pop after put: [1]
# ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
# Pop before put: []
# First pop after put: [1]
# Second pop after put: []
# ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin13.0.0]
# Pop before put: []
# First pop after put: [1]
# Second pop after put: []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment