| # 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 |