Created
September 27, 2024 16:10
-
-
Save HarlemSquirrel/666f4e2e409990a14fabbc06b4e1cb5e to your computer and use it in GitHub Desktop.
An example use case for make multi-threaded HTTP requests and using shared variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "json" | |
| require "net/http" | |
| require "ostruct" | |
| require "amazing_print" | |
| user_summaries = [] | |
| req_total_ms = 0 | |
| host = "api.github.com" | |
| usernames = %w[ | |
| dhh | |
| HarlemSquirrel | |
| jarednorman | |
| matz | |
| matzbot | |
| nateberkopec | |
| octocat | |
| tenderlove | |
| ] | |
| Thread.abort_on_exception = true | |
| semaphore = Thread::Mutex.new | |
| puts "Getting #{usernames.size} user summaries..." | |
| start_time = Time.now | |
| threads = usernames.map do |username| | |
| Thread.new do | |
| Net::HTTP.start(host, use_ssl: true) do |http| | |
| path = "/users/#{username}" | |
| req_start_time = Time.now | |
| response = http.get(path) | |
| duration_ms = ((Time.now - req_start_time) * 1000).round(0) | |
| parsed_response_body = JSON.parse(response.body) | |
| user = OpenStruct.new(parsed_response_body) | |
| user_summary = { | |
| username: username, | |
| location: user.location, | |
| followers: user.followers, | |
| blog: user.blog, | |
| response_ms: duration_ms, | |
| } | |
| # Access shared variables | |
| semaphore.synchronize do | |
| user_summaries << user_summary | |
| req_total_ms += duration_ms | |
| end | |
| end | |
| end | |
| end | |
| threads.each(&:join) | |
| real_time_ms = ((Time.now - start_time) * 1000).round(0) | |
| puts user_summaries | |
| puts "\nLoaded #{user_summaries.size} summaries in #{real_time_ms}ms" | |
| puts "Total request time: #{req_total_ms}ms" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment