Created
May 17, 2020 05:04
-
-
Save ioquatix/0b61246e91f94071d5c8c6f9b68e6107 to your computer and use it in GitHub Desktop.
Threads vs Processes with the GVL
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
# gem install async-container | |
gem "async-container" | |
require 'async/clock' | |
require 'async/container' | |
def fibonacci(n) | |
if n < 2 | |
return n | |
else | |
return fibonacci(n-1) + fibonacci(n-2) | |
end | |
end | |
def work(*) | |
puts fibonacci(32) | |
end | |
def measure_work(container, **options, &block) | |
duration = Async::Clock.measure do | |
container.run(**options, &block) | |
container.wait | |
end | |
puts "Duration for #{container.class}: #{duration}" | |
end | |
threaded = Async::Container::Threaded.new | |
measure_work(threaded, count: 20, &self.method(:work)) | |
forked = Async::Container::Forked.new | |
measure_work(forked, count: 20, &self.method(:work)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a work for being I/O bound: