Last active
February 12, 2022 17:42
-
-
Save havenwood/8a430463f0f452d166131a7232565335 to your computer and use it in GitHub Desktop.
Just a overly simplified example of calling methods without arguments concurrently with Async and in parallel with Ractors
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
require 'async' | |
module Task | |
module_function | |
def async(*method_names) | |
Async do | |
method_names.map do |job| | |
Async do | |
job.to_proc.call(self) | |
end | |
end | |
end.result | |
end | |
def parallel(*method_names) | |
method_names.map do |method_name| | |
Ractor.new method_name do |name| | |
name.to_proc.call(binding) | |
end | |
end | |
end | |
end | |
## | |
# Just an example of calling methods without arguments | |
# concurrently with Async and in parallel with Ractors | |
def foo | |
sleep 1 | |
puts 'foo' | |
end | |
def bar | |
sleep 2 | |
puts 'bar' | |
end | |
Task.async(:foo, :foo, :bar).size | |
#>> foo | |
#>> foo | |
#>> bar | |
#=> 3 | |
Task.parallel(:foo, :foo, :bar).size | |
#=> 3 | |
#>> foo | |
#>> foo | |
#>> bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment