tubes 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tubes.rb +45 -33
- metadata +1 -1
data/lib/tubes.rb
CHANGED
@@ -27,7 +27,7 @@ class Tube
|
|
27
27
|
@order = options.delete(:order) || ''
|
28
28
|
|
29
29
|
@input = options.delete(:input)
|
30
|
-
@output =
|
30
|
+
@output = serial? ? nil : []
|
31
31
|
|
32
32
|
@threads = []
|
33
33
|
|
@@ -75,17 +75,23 @@ class Tube
|
|
75
75
|
output_file = segment_cache self, step
|
76
76
|
if File.exists?(output_file)
|
77
77
|
self.puts "Skipping: #{step}"
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
output = JSON.load(File.read(output_file))["data"]
|
79
|
+
|
80
|
+
if parallel?
|
81
|
+
@thread_lock.synchronize do
|
82
|
+
@output << output
|
83
|
+
end
|
84
|
+
elsif serial?
|
85
|
+
@output = output
|
86
|
+
@input = output
|
81
87
|
end
|
82
88
|
else
|
83
89
|
self.puts "Running: #{step}"
|
84
90
|
|
85
|
-
if
|
91
|
+
if serial?
|
86
92
|
dispatch(segment, output_file, *args)
|
87
93
|
@input = @output
|
88
|
-
elsif
|
94
|
+
elsif parallel?
|
89
95
|
thread = Thread.new(@thread_lock) do |lock|
|
90
96
|
Thread.current[:lock] = lock
|
91
97
|
Thread.current.abort_on_exception = true
|
@@ -117,28 +123,35 @@ class Tube
|
|
117
123
|
|
118
124
|
private
|
119
125
|
|
126
|
+
def serial?
|
127
|
+
@type == :serial
|
128
|
+
end
|
129
|
+
|
130
|
+
def parallel?
|
131
|
+
@type == :parallel
|
132
|
+
end
|
133
|
+
|
120
134
|
def tube(mode, args=nil, &block)
|
121
135
|
begin
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
tube = child(mode, args)
|
129
|
-
tube.instance_eval &block
|
130
|
-
tube.threads.each { |thread| thread.join } # Could be a parallel block inside a parallel block.
|
131
|
-
@thread_lock.synchronize do
|
132
|
-
@output << mode == :parallel ? tube.output.flatten(1) : tube.output
|
133
|
-
end
|
134
|
-
end
|
135
|
-
@threads << thread
|
136
|
-
when :serial # When inside serial.
|
136
|
+
if parallel? # When inside parallel.
|
137
|
+
thread = Thread.new(@thread_lock) do |lock|
|
138
|
+
Thread.current[:lock] = lock
|
139
|
+
Thread.current.abort_on_exception = true
|
140
|
+
|
137
141
|
tube = child(mode, args)
|
138
142
|
tube.instance_eval &block
|
139
|
-
tube.threads.each { |thread| thread.join }
|
140
|
-
@
|
141
|
-
|
143
|
+
tube.threads.each { |thread| thread.join } # Could be a parallel block inside a parallel block.
|
144
|
+
@thread_lock.synchronize do
|
145
|
+
@output << mode == :parallel ? tube.output.flatten(1) : tube.output
|
146
|
+
end
|
147
|
+
end
|
148
|
+
@threads << thread
|
149
|
+
elsif serial?
|
150
|
+
tube = child(mode, args)
|
151
|
+
tube.instance_eval &block
|
152
|
+
tube.threads.each { |thread| thread.join }
|
153
|
+
@output = mode == :parallel ? tube.output.flatten(1) : tube.output
|
154
|
+
@input = @output
|
142
155
|
end
|
143
156
|
rescue => e
|
144
157
|
@exception = e
|
@@ -180,9 +193,9 @@ class Tube
|
|
180
193
|
f.write({:data => output}.to_json)
|
181
194
|
end
|
182
195
|
|
183
|
-
if
|
196
|
+
if serial?
|
184
197
|
@output = output
|
185
|
-
elsif
|
198
|
+
elsif parallel?
|
186
199
|
@thread_lock.synchronize do
|
187
200
|
@output << output
|
188
201
|
end
|
@@ -196,13 +209,12 @@ class Tube
|
|
196
209
|
|
197
210
|
|
198
211
|
def child(type, args=nil)
|
199
|
-
order =
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
"#{@order}P#{@parallel_count}"
|
212
|
+
order = if type == :serial
|
213
|
+
@serial_count += 1
|
214
|
+
"#{@order}S#{@serial_count}"
|
215
|
+
elsif type == :parallel
|
216
|
+
@parallel_count += 1
|
217
|
+
"#{@order}P#{@parallel_count}"
|
206
218
|
end
|
207
219
|
|
208
220
|
Tube.new(@dir, :type => type, :input => args || @input, :parent => self, :order => order, :started_at => started_at)
|