Last active
July 31, 2021 22:02
-
-
Save dux/466507a5e86cadd2c4714381a1f06cf4 to your computer and use it in GitHub Desktop.
Simple threaded/promisse-like runner for Ruby
This file contains hidden or 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
class Thread::Simple | |
attr_accessor :que, :size, :named | |
def initialize size: 5, sleep: 0.01 | |
@sync = Mutex.new | |
@sleep = sleep | |
@size = size | |
@que = [] | |
@threds = [] | |
@named = {} | |
end | |
def add name = nil, &block | |
@sync.synchronize do | |
if name | |
@que << proc { @named[name] = block.call } | |
else | |
@que << block | |
end | |
end | |
end | |
def run endless: false | |
@endless = endless | |
@size.times do | |
@threds << Thread.new do | |
task = nil | |
while active? | |
@sync.synchronize { task = @que.pop } | |
task.call if task | |
sleep @sleep | |
end | |
end | |
end | |
while active? | |
sleep @sleep | |
end | |
@threds.each(&:join) | |
end | |
def stop | |
@endless = false | |
end | |
def [] name | |
@named[name] | |
end | |
private | |
def active? | |
@endless || @que.first | |
end | |
end |
This file contains hidden or 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
# run 2 slow tasks in parralel, show results when finished | |
t = Threaded.new | |
t.add(:rake) { `rake -T` } | |
t.add(:capistrano) { `cap -T` } | |
t.run | |
puts 'Rake tasks:' | |
puts ' ' + t[:rake].gsub($/, "\n ") | |
puts 'Capinstrano tasks:' | |
puts ' ' + t[:capistrano].gsub($/, "\n ") | |
# OUTPUT | |
# Rake tasks: | |
# ... | |
# | |
# Capinstrano tasks: | |
# ... |
This file contains hidden or 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
# run 10 threads with random sleep, pass the data | |
t = Threaded.new 5 | |
for i in (1..10).to_a | |
t.add i do |_i| | |
num = rand()*10 | |
text = "i: #{_i}, rand: #{num}" | |
sleep num | |
puts text | |
text | |
end | |
end | |
t.run # run jobs in que and exit | |
t.run endless: true # never stop and wait for jobs | |
puts | |
puts '>%s<' % t[5] | |
# OUTPUT | |
# i: 4, rand: 2.201648777839252 | |
# i: 2, rand: 3.8469286392171265 | |
# i: 1, rand: 4.025847841724484 | |
# i: 6, rand: 2.0530587142893273 | |
# i: 7, rand: 0.876026456103115 | |
# i: 3, rand: 4.742388503989174 | |
# i: 5, rand: 6.399928463357492 | |
# i: 9, rand: 7.61068266780084 | |
# i: 10, rand: 8.035897901169573 | |
# i: 8, rand: 8.865822652849436 | |
# | |
# >i: 5, rand: 6.399928463357492< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment