Sha256: a4345fb82d4dbcc1bbf048f188b74ae5ecc44567d37a0690a74e4785475847f4
Contents?: true
Size: 1 KB
Versions: 6
Compression:
Stored size: 1 KB
Contents
#!/usr/bin/env ruby # frozen_string_literal: true require_relative '../lib/async' module Async::Methods def sleep(*args) Async::Task.current.sleep(*args) end def async(name) original_method = self.method(name) define_method(name) do |*args| Async::Reactor.run do |task| original_method.call(*args) end end end def await(&block) block.call.wait end def barrier! Async::Task.current.children.each(&:wait) end end include Async::Methods async def count_chickens(area_name) 3.times do |i| sleep rand puts "Found a chicken in the #{area_name}!" end end async def find_chicken(areas) puts "Searching for chicken..." sleep rand * 5 return areas.sample end async def count_all_chckens # These methods all run at the same time. count_chickens("garden") count_chickens("house") count_chickens("tree") # Wait for all previous async work to complete... barrier! puts "There was a chicken in the #{find_chicken(["garden", "house", "tree"]).wait}" end count_all_chckens
Version data entries
6 entries across 6 versions & 1 rubygems