Skip to content

Instantly share code, notes, and snippets.

@palkan palkan/async_await.rb Secret
Last active Jun 9, 2018

Embed
What would you like to do?
[Saint-P RubyConf 2018] AsyncAwait in Ruby
# NOTE: gem install asynchronize
require "asynchronize"
module AsyncAwait
refine Module do
def async(mid)
include Asynchronize unless included_modules.include?(Asynchronize)
asynchronize mid
end
end
refine Kernel do
def await(*ths)
if ths.size == 1
ths.first.value
else
ths.map(&:value)
end
end
end
end
require "benchmark"
using AsyncAwait
class A
async def sleep!
t = rand * 2
sleep t
t
end
end
a = A.new
t = Benchmark.measure do
p await(a.sleep!, a.sleep!).sum
end
p t.real
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.