Created
October 1, 2020 01:04
-
-
Save havenwood/f267ab57b01f009c356980c688dc499d to your computer and use it in GitHub Desktop.
Example of async open-uri with Ruby 3. See https://www.codeotaku.com/journal/2020-06/open-source-progress-report/index
This file contains 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 'async' | |
require 'async/barrier' | |
require 'open-uri' | |
def async_fetch(*urls) | |
barrier = Async::Barrier.new | |
Async do | |
urls.map do |url| | |
barrier.async { URI.open url, &:read } | |
end.tap { barrier.wait } | |
end.result.map(&:result) | |
end | |
url = 'https://api.kanye.rest?format=text' | |
puts async_fetch url, url, url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment