28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/particle_pool/pool_thread.rb', line 28
def start
tasks = []
iter = tasks.each
loop do
nt = []
shouldpop = lambda do
return true unless @queue.empty?
return true if tasks.empty? && nt.empty?
false
end
shouldblock = lambda do
return true if @queue.empty? && (!tasks.empty? || !nt.empty?)
false
end
while shouldpop.()
begin
i = @queue.pop shouldblock.()
nt << i
rescue ThreadError
break
end
end
nt.each do |i|
begin
t = i[:task]
t.start(*i[:args], **i[:kwargs])
tasks << t
rescue => e
puts e.backtrace
puts e
end
end
begin
t = iter.next
t.resume
if t.done?
@tasks -= 1
tasks.delete t
end
rescue StopIteration
iter.rewind
end
end
end
|