Skip to content

Commit

Permalink
Add idler wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 2, 2024
1 parent 13d3f47 commit 3533633
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/async/idler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

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

module Async
class Idler
def initialize(parent: Task.current)
@parent = parent
@list = List.new
end

def async(*arguments, parent: (@parent or Task.current), **options)
wait

parent.async(*arguments, **options)
end

def wait

end
end
end
4 changes: 4 additions & 0 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ def initialize(parent = nil, selector: nil)
@interrupted = false

@blocked = 0
@load = 0

@timers = ::Timers::Group.new
end

# The load is the number of tasks that were ready to run in the last iteration of the event loop.
attr :load

def scheduler_close
# If the execution context (thread) was handling an exception, we want to exit as quickly as possible:
unless $!
Expand Down
33 changes: 33 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby

require_relative 'lib/async'

def measure_load(sample_duration = 1.0)
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
sleep(sample_duration)
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time

return (duration / sample_duration) - 1.0
end

def add_load(n = 100)
n.times do
Async do
while true
sleep 0.01
end
end
end
end

Async do |task|
while true
load = measure_load
puts "Load: #{sprintf("%0.3f%%", load * 100.0)} children: #{task.children&.size}"

if load <= 0.01
puts "Adding load..."
add_load
end
end
end

0 comments on commit 3533633

Please sign in to comment.