Q:

How to process single training file in parallel

I have a file train.csv that contains paths to images and their labels. ie:
img1.jpg 3
img2.jpg 1
...

After going through the reading data tutorial I came up with some code to go through each image, resize it and apply distortions:
def apply_distortions(resized_image):
    # do a bunch of tf.image distortion...
    return float_image

def processing(filename):
    file_contents = tf.read_file(filename)
    image = tf.image.decode_jpeg(file_contents, channels=3)
    resized_image = tf.image.resize_images(image, 299, 299)
    distorted_image = apply_distortions(resized_image)
    return distorted_image

def parse_csv(filename_queue):
    line_reader = tf.TextLineReader()
    key, line = line_reader.read(filename_queue)
    filename, label = tf.decode_csv(line,     # line_batch or line (depending if you want to batch)
                               record_defaults=[tf.constant([],dtype=tf.string),
                                                tf.constant([],dtype=tf.int32)],
                               field_delim=' ')
    processed_image = processing(filename)
    return processed_image, label

The problem now is that I'm confused how to do these operations across the file in parallel. The documentation suggests either using tf.train.batch_join or tf.train.batch with num_threads=N.
I first tried following the example code using tf.train.batch_join but this seems to be intended for processing multiple files in parallel. In my case however I just have 1 file. 
filename_queue = tf.train.string_input_producer(["train.txt"], num_epochs=1, shuffle=True)    
example_list = [parse_csv(filename_queue) for _ in range(8)]
example_batch, label_batch = tf.train.batch_join(example_list, batch_size)

I also tried setting tf.train.batch([example, label], batch_size, num_threads=8) but its not clear to me if this is doing the right thing (although I can see more cpu cores in use)
filename_queue = tf.train.string_input_producer(["train.txt"], num_epochs=1, shuffle=True)
example, label = parse_csv(filename_queue)
example_batch, label_batch = tf.train.batch([example, label], batch_size, num_threads=8)

Here is my code for executing the graph:
sess.run(tf.initialize_all_variables())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess,coord)
try:
    while not coord.should_stop():
        X, Y = sess.run([example_batch, label_batch])
        # Now run a training step
except tf.errors.OutOfRangeError:
    print('Done training -- epoch limit reached')
finally:
    # When done, ask the threads to stop.
    coord.request_stop()
coord.join(threads)
sess.close()

Whats the best way to process this file in parallel?

A:

Both seem like viable approaches. Using batch with threads=N will create N copies of your reader op connected to your queue so that they can run in parallel, while batch_join you have to create the copies manually.
In your usage with batch_join you are creating several copies of TextLineReader which (as you noticed) will only parallelize across files. To have several threads reading a single file, you could instead create one TextLineReader and have several line_reader.read ops using the same reader.
Here's an example with some textfiles containing numbers
Generate numbers:
num_files=10
num_entries_per_file=10
file_root="/temp/pipeline"
os.system('mkdir -p '+file_root)
for fi in range(num_files):
  fname = file_root+"/"+str(fi)
  dump_numbers_to_file(fname, fi*num_entries_per_file, (fi+1)*num_entries_per_file)

Read those numbers in batches of size 2, with parallelism of 2
ops.reset_default_graph()
filename_queue = tf.train.string_input_producer(["/temp/pipeline/0",
                                                 "/temp/pipeline/1"],
                                                shuffle=False)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
numeric_val1, = tf.decode_csv(value, record_defaults=[[-1]])
numeric_val2, = tf.decode_csv(value, record_defaults=[[-1]])
numeric_batch = tf.batch_join([[numeric_val1,], [numeric_val2]], 2)
# have to create session before queue runners because they use default session
sess = create_session()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

print '\n'.join([t.name for t in threads])
for i in range(20):
  print sess.run([numeric_batch])

coord.request_stop()
coord.join(threads)

You may see something like this:
QueueRunner(input_producer:input_producer/input_producer_EnqueueMany)
QueueRunner(input_producer:input_producer/input_producer_Close_1)
QueueRunner(batch_join/fifo_queue:batch_join/fifo_queue_enqueue)
QueueRunner(batch_join/fifo_queue:batch_join/fifo_queue_enqueue_1)
QueueRunner(batch_join/fifo_queue:batch_join/fifo_queue_Close_1)
[array([0, 1], dtype=int32)]
[array([2, 3], dtype=int32)]
[array([4, 5], dtype=int32)]
[array([6, 7], dtype=int32)]
[array([8, 9], dtype=int32)]
[array([10, 11], dtype=int32)]
[array([12, 13], dtype=int32)]
[array([14, 15], dtype=int32)]
[array([16, 17], dtype=int32)]
[array([18, 19], dtype=int32)]

From the list of threads, you can see that there are 2 threads corresponding to read operations (fifo_queue_enqueue and fifo_queue_enqueue_1 so you can do 2 reads in parallel)

