Hello,
I have a ruby script which uses a thread pool to fetch JSON objects via
http. Now, I’m using a static number of pages (see pseudo-code below) but I
need to introduce a condition and stop the process when that condition is
met (e.g. request returns a 404).
Much depends on which "process" you're stopping. More below.
The structure now looks like this:
1.upto(100) do |n|
threads << Thread.new { ... }
end
threads.join()
What I don’t understand is how to apply a sort of break when a condition
is met. I’m thinking something along the lines:
x = true
while x
threads << Thread.new {
…
break unless x
}
end
threads.join()
Will the above approach work? Is there a better one? How do people deal in
these situations with threads?
I gather what you want to do is: stop spawning threads when you find a 404.
The problem is, `while true; Thread.new; end` runs very fast. You'll end up
with hundreds or thousands of threads queued up before the first one can
read a response (and detect a 404.) If it was me, I'd combine the two
approaches, wrapping the thread pool inside the while-loop; something like:
x = true
while x
1.upto(100) do |n|
threads << Thread.new do
# ...fetch JSON...
x = false if status==404?
end
end
threads.join()
end
That way you only have at most 100 threads (in the pool) wasting time
returning 404s. Personally I'd turn that number way down, maybe 10 or so,
depending on your circumstances (network bandwidth and latency, available
memory, processor speed, etc.)
Incidentally, I made a gem many years ago just for this sort of situation:
require 'threadpuddle'
tp = ThreadPuddle.new 10
x = true
while x
tp.spawn do
break unless x
# ...fetch JSON...
x = false if status==404?
end
end
tp.join
Does that look like what you're trying to do?
thanks!
No worries, I hope it helps.
Cheers.
···
On 29 March 2016 at 21:15, Panagiotis Atmatzidis <atma@convalesco.org> wrote:
--
Matthew Kerwin
http://matthew.kerwin.net.au/