Last active
March 5, 2021 02:23
-
-
Save oprypin/9915def54564a7c44d53f15eaa1ab9b6 to your computer and use it in GitHub Desktop.
Revisions
-
oprypin revised this gist
May 3, 2018 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,8 @@ end require "http/client" urls = ["https://example.com/", "https://crystal-lang.org"] pages = paralleln(urls) do |url| HTTP::Client.get(url) end p pages -
oprypin revised this gist
May 3, 2018 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,23 @@ def paralleln(items : Indexable(T), &block : T -> R) forall T, R results = Array(R).new(items.size) { r = uninitialized R } done = Channel(Exception?).new items.each_with_index do |item, i| spawn do begin results[i] = block.call(item) rescue e done.send e else done.send nil end end end items.each do if (exc = done.receive) raise exc end end results -
oprypin revised this gist
May 3, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ def paralleln(items : Indexable(T), &block : T -> R) forall T, R end items.size.times do done.receive end results -
oprypin created this gist
May 3, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ def paralleln(items : Indexable(T), &block : T -> R) forall T, R results = Array(R).new(items.size) { r = uninitialized R } done = Channel(Nil).new items.each_with_index do |item, i| spawn do begin results[i] = block.call(item) ensure done.send nil end end end items.size.times do done.receive end results end require "http/client" pages = paralleln(["https://example.com/", "https://crystal-lang.org"]) { |url| HTTP::Client.get(url) } p pages