-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add idler wip.Add
Scheduler#load
and Async::Idler
for scheduling …
…tasks when idle. - `Async::Idler` introduces a `maximum_load` and functions like a semaphore, in that it will schedule tasks until the maximum load is reached.
- Loading branch information
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require_relative '../../lib/async' | ||
require_relative '../../lib/async/idler' | ||
|
||
Async do | ||
idler = Async::Idler.new(0.8) | ||
|
||
Async do | ||
while true | ||
idler.async do | ||
puts "Starting load..." | ||
while true | ||
sleep 0.1 | ||
end | ||
end | ||
end | ||
end | ||
|
||
scheduler = Fiber.scheduler | ||
while true | ||
load = scheduler.load | ||
|
||
puts "Load: #{load}..." | ||
sleep 1.0 | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2024, by Samuel Williams. | ||
|
||
module Async | ||
class Idler | ||
def initialize(maximum_load = 0.9, backoff: 0.001, parent: nil) | ||
@maximum_load = maximum_load | ||
@backoff = backoff | ||
@parent = parent | ||
|
||
@waiting = 0 | ||
end | ||
|
||
def async(*arguments, parent: (@parent or Task.current), **options, &block) | ||
wait | ||
|
||
parent.async(*arguments, **options, &block) | ||
end | ||
|
||
def wait | ||
scheduler = Fiber.scheduler | ||
|
||
if @waiting > 0 | ||
scheduler.yield | ||
@waiting = 0 | ||
else | ||
@waiting += 1 | ||
end | ||
|
||
while scheduler.load > @maximum_load | ||
sleep(@backoff) | ||
end | ||
ensure | ||
@waiting = 0 | ||
end | ||
end | ||
end |
This file contains 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