Skip to content

Instantly share code, notes, and snippets.

@JEG2
Created January 22, 2016 16:33
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 JEG2/8cdab89ba228a70e6d6c to your computer and use it in GitHub Desktop.
Save JEG2/8cdab89ba228a70e6d6c to your computer and use it in GitHub Desktop.
Ruby's concurrency, an example.
require "open-uri"
require "json"
require "benchmark"
REPO_URL = "https://api.github.com/users/JEG2/repos"
def github_api(url)
open(url) { |response| JSON.parse(response.read) }
end
concurrency = (ARGV.shift || 10).to_i
serial = Benchmark.realtime do
jeg2 = github_api(REPO_URL)
jeg2.first(concurrency).each do |repo|
name = github_api(repo["url"])["full_name"]
puts "Fetched #{name} repository."
end
end
puts "Serial time: #{serial}"
concurrent = Benchmark.realtime do
urls = Queue.new
names = Queue.new
threads = Array.new(concurrency) {
Thread.new do
loop do
url = urls.deq
data = github_api(url)
if data.is_a?(Array) # repo list
data.first(concurrency).each do |repo|
q.enq(repo["url"])
end
else
names.enq(data["full_name"])
end
end
end
}
urls.enq(REPO_URL)
concurrency.times do
name = names.deq
puts "Fetched #{name} repository."
end
end
puts "Concurrent time: #{concurrent}"
# >> Fetched JEG2/advent_of_code_2015 repository.
# >> Fetched JEG2/amalgalite repository.
# >> Fetched JEG2/asciimation repository.
# >> Fetched JEG2/attachment_fu repository.
# >> Fetched JEG2/better_bj repository.
# >> Fetched JEG2/bird_of_paradise repository.
# >> Fetched JEG2/bottles_of_beer_song repository.
# >> Fetched JEG2/broadsides repository.
# >> Fetched JEG2/browser_captcha repository.
# >> Fetched JEG2/challenges_for_game_designers repository.
# >> Serial time: 3.4831192519995966
# >> Fetched JEG2/bottles_of_beer_song repository.
# >> Fetched JEG2/bird_of_paradise repository.
# >> Fetched JEG2/better_bj repository.
# >> Fetched JEG2/asciimation repository.
# >> Fetched JEG2/advent_of_code_2015 repository.
# >> Fetched JEG2/broadsides repository.
# >> Fetched JEG2/challenges_for_game_designers repository.
# >> Fetched JEG2/amalgalite repository.
# >> Fetched JEG2/browser_captcha repository.
# >> Fetched JEG2/attachment_fu repository.
# >> Concurrent time: 0.7062488960000337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment