lib/onix/reader.rb in onix-0.4.0 vs lib/onix/reader.rb in onix-0.4.2
- old
+ new
@@ -66,21 +66,25 @@
end
@product_klass = product_klass
# create a sized queue to store each product read from the file
+ # We use a separate thread to read products from the source file.
+ # This queue is a thread-safe way to transfer products from that
+ # thread back into the main one.
@queue = SizedQueue.new(100)
- # launch a reader thread to process the file and store each
- # product in the queue
+ # launch a reader thread
Thread.abort_on_exception = true
Thread.new { read_input }
- # TODO: this is a seriously hacky way to ensure the reading thread
- # has enough time to read our metadata and header objects from
- # the input stream. I should be making the constructor block until
- # it has actively confirmed the data has been read
- sleep 1
+ # don't return from the constructor until the reading thread
+ # has spun up and put at least one item into the queue. If
+ # it finds no Products in the file, it queues a nil, so we
+ # shouldn't get stuck here indefinitely
+ while @queue.size == 0
+ sleep 0.05
+ end
end
# Iterate over all the products in an ONIX file
#
def each(&block)