Skip to content

Commit

Permalink
Rubocop fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 24, 2024
1 parent f39905f commit b8c90e1
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 49 deletions.
48 changes: 24 additions & 24 deletions benchmark/rubies/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions benchmark/timers/gems.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

Expand Down
32 changes: 24 additions & 8 deletions examples/count/fibers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
32 changes: 24 additions & 8 deletions examples/count/threads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
13 changes: 7 additions & 6 deletions examples/debug/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions examples/dining-philosophers/philosophers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

Expand Down
4 changes: 2 additions & 2 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b8c90e1

Please sign in to comment.