diff --git a/benchmark/rubies/benchmark.rb b/benchmark/rubies/benchmark.rb index 4a899846..e0aea7b3 100755 --- a/benchmark/rubies/benchmark.rb +++ b/benchmark/rubies/benchmark.rb @@ -29,36 +29,36 @@ def b # Fiber reactor code taken from # https://www.codeotaku.com/journal/2018-11/fibers-are-the-right-solution/index class Reactor - def initialize - @readable = {} - @writable = {} - end + def initialize + @readable = {} + @writable = {} + end - def run - while @readable.any? or @writable.any? - readable, writable = IO.select(@readable.keys, @writable.keys, []) + def run + while @readable.any? or @writable.any? + readable, writable = IO.select(@readable.keys, @writable.keys, []) - readable.each do |io| - @readable[io].resume - end + readable.each do |io| + @readable[io].resume + end - writable.each do |io| - @writable[io].resume - end - end + writable.each do |io| + @writable[io].resume + end end + end - def wait_readable(io) - @readable[io] = Fiber.current - Fiber.yield - @readable.delete(io) - end + def wait_readable(io) + @readable[io] = Fiber.current + Fiber.yield + @readable.delete(io) + end - def wait_writable(io) - @writable[io] = Fiber.current - Fiber.yield - @writable.delete(io) - end + def wait_writable(io) + @writable[io] = Fiber.current + Fiber.yield + @writable.delete(io) + end end class Wrapper diff --git a/benchmark/timers/gems.rb b/benchmark/timers/gems.rb index 6272275a..4bca387e 100644 --- a/benchmark/timers/gems.rb +++ b/benchmark/timers/gems.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Released under the MIT License. # Copyright, 2024, by Samuel Williams. diff --git a/examples/count/fibers.rb b/examples/count/fibers.rb index 4e5eb4ae..46115bb1 100755 --- a/examples/count/fibers.rb +++ b/examples/count/fibers.rb @@ -11,24 +11,40 @@ puts "=========== FIBERS ===========" puts + count = 0 time = Benchmark.measure do Sync do 5.times do [ - Async do ; transitions << "A1" - puts "Task 1: count is #{count}" ; transitions << "A2" - count += 1 ; transitions << "A3" - sleep(0.1) ; transitions << "A4" + Async do + transitions << "A1" + puts "Task 1: count is #{count}" + + transitions << "A2" + count += 1 + + transitions << "A3" + sleep(0.1) + + transitions << "A4" end, - Async do ; transitions << "B1" - puts "Task 2: count is #{count}" ; transitions << "B2" - count += 1 ; transitions << "B3" - sleep(0.1) ; transitions << "B4" + Async do + transitions << "B1" + puts "Task 2: count is #{count}" + + transitions << "B2" + count += 1 + + transitions << "B3" + sleep(0.1) + + transitions << "B4" end ].map(&:wait) end end end + puts "#{time.real.round(2)} seconds to run. Final count is #{count}" puts transitions.join(" ") diff --git a/examples/count/threads.rb b/examples/count/threads.rb index 98226102..6c9b2cc0 100755 --- a/examples/count/threads.rb +++ b/examples/count/threads.rb @@ -10,22 +10,38 @@ puts "=========== THREADS ===========" puts + count = 0 time = Benchmark.measure do 5.times do [ - Thread.new do ; transitions << "A1" - puts "Task 1: count is #{count}" ; transitions << "A2" - count += 1 ; transitions << "A3" - sleep(0.1) ; transitions << "A4" + Thread.new do + transitions << "A1" + puts "Task 1: count is #{count}" + + transitions << "A2" + count += 1 + + transitions << "A3" + sleep(0.1) + + transitions << "A4" end, - Thread.new do ; transitions << "B1" - puts "Task 2: count is #{count}" ; transitions << "B2" - count += 1 ; transitions << "B3" - sleep(0.1) ; transitions << "B4" + Thread.new do + transitions << "B1" + puts "Task 2: count is #{count}" + + transitions << "B2" + count += 1 + + transitions << "B3" + sleep(0.1) + + transitions << "B4" end ].map(&:join) end end + puts "#{time.real.round(2)} seconds to run. Final count is #{count}" puts transitions.join(" ") diff --git a/examples/debug/sample.rb b/examples/debug/sample.rb index e9d82114..117c67ff 100644 --- a/examples/debug/sample.rb +++ b/examples/debug/sample.rb @@ -6,10 +6,11 @@ require 'async' Async do |t| - t.async do - puts "1\n" - end - t.async do - puts "2\n" - end + t.async do + puts "1\n" + end + + t.async do + puts "2\n" + end end diff --git a/examples/dining-philosophers/philosophers.rb b/examples/dining-philosophers/philosophers.rb index 0fb9f7a8..6f70a9c9 100755 --- a/examples/dining-philosophers/philosophers.rb +++ b/examples/dining-philosophers/philosophers.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + # Released under the MIT License. # Copyright, 2024, by Samuel Williams. diff --git a/lib/async/task.rb b/lib/async/task.rb index bc30349e..7411bd8f 100644 --- a/lib/async/task.rb +++ b/lib/async/task.rb @@ -192,8 +192,8 @@ def run(*arguments) # I'm not completely happy with this overhead, but the alternative is to not log anything which makes debugging extremely difficult. Maybe we can introduce a debug wrapper which adds extra logging. if @finished.nil? Console::Event::Failure.for(error).emit(self, "Task may have ended with unhandled exception.", severity: :warn) - # else - # Console::Event::Failure.for(error).emit(self, severity: :debug) + else + # Console::Event::Failure.for(error).emit(self, severity: :debug) end raise diff --git a/test/async/task.rb b/test/async/task.rb index 5df64298..ff422b57 100644 --- a/test/async/task.rb +++ b/test/async/task.rb @@ -609,7 +609,7 @@ def after # This can invoke `io_wait`, which previously had `rescue TimeoutError`, causing the timeout to be ignored. task.with_timeout(0.1) {input.gets} rescue Async::TimeoutError => error - # Ignore. + # Ignore. end end