Skip to content

Instantly share code, notes, and snippets.

@collindonnell
Created April 8, 2022 21:47
Show Gist options
  • Save collindonnell/ab8d105392fd3fe551df4da3c69d33d7 to your computer and use it in GitHub Desktop.
Save collindonnell/ab8d105392fd3fe551df4da3c69d33d7 to your computer and use it in GitHub Desktop.
Ruby Async Demo
#!/usr/bin/env ruby
require "async"
# Simple method to measure how long an operation takes.
def measure_time
start = Time.now
yield
puts "Duration: #{Time.now - start}"
end
def perform_sync
sleep 1
sleep 1
end
def perform_async
Async do |task|
task.async do
sleep 1
end
task.async do
sleep 1
end
end
end
measure_time do
puts "Synchronous..."
perform_sync
end
measure_time do
puts "Async..."
perform_async
end
# Synchronous...
# Duration: 2.002028
# Async...
# Duration: 1.000946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment