-
-
Save jj1bdx/0e1a7c5b7bf4f3c857eeb3628eaee693 to your computer and use it in GitHub Desktop.
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
def async(name, &b) | |
define_method name do |*args| | |
f = Fiber.new{ | |
r = b.call(*args) | |
f.instance_variable_set(:@result, r) | |
} | |
def f.value | |
while true | |
unless self.alive? | |
break @result | |
else | |
self.resume | |
end | |
end | |
end | |
f | |
end | |
end | |
require 'fiber' | |
def await task | |
while true | |
r = task.resume | |
if task.alive? | |
Fiber.yield | |
else | |
break r | |
end | |
end | |
end | |
def do_something1 | |
end | |
def do_something2 | |
end | |
async :task2 do | |
do_something1 | |
Fiber.yield | |
do_something2 | |
end | |
async :task1 do |name| | |
await task2() | |
"hello #{name}" | |
end | |
task1_instance = task1("ko1") | |
p task1_instance.value | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://twitter.com/_ko1/status/870473966120390656