Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active February 12, 2022 17:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/8a430463f0f452d166131a7232565335 to your computer and use it in GitHub Desktop.
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
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