Skip to content

Instantly share code, notes, and snippets.

@simi
Created May 24, 2022 13:29
Show Gist options
  • Save simi/1547d5f01d86a8caa8caf952a2d80c5a to your computer and use it in GitHub Desktop.
Save simi/1547d5f01d86a8caa8caf952a2d80c5a to your computer and use it in GitHub Desktop.
require 'thread'
require 'logger'
POOL_SIZE = 10 # limit for HTTP parallel connections
QUEUE_SIZE = 5 # limit to consume only fixed amount of memory
queue = SizedQueue.new(QUEUE_SIZE)
logger = Logger.new($stdout)
END_MESSAGE = :done
threads = POOL_SIZE.times.map do |t|
Thread.new do
10.times do |i|
# imagine HTTP request here I want to do in parallel and pushing result to the queue
queue.push(:hello)
logger.info "T#{t} pushed message #{i}"
sleep rand
end
queue.push(END_MESSAGE)
logger.info "T#{t} done"
end
end
finished = 0
consumed = 0
loop do
message = queue.pop
if message == END_MESSAGE
finished = finished + 1
logger.info "done, finished #{finished} threads"
break if finished == pool_size
else
consumed = consumed + 1
logger.info "pop, consumed #{consumed} messages"
end
end
logger.info "all done, consumed #{consumed} messages, finished #{finished} threads"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment