file io - How to handle thread returns Ruby -
i have ruby script in i'm parsing large csv file. ihave handled , working well, except how deal thread's return values. have:
length = (ses.size/4).ceil ses.each_slice(length) |x| threads << thread.new { a,b = splat x } end threads.each { |thr| thr.join }
'splat' returns temp files need appended output files out1 , out2. i'm stumbling on that/how information. if point me in right direction that'd great.
two things, first, when pass 'x' thread, it's safer make thread-local changing this:
threads << thread.new { a,b = splat x }
into this:
threads << thread.new(x) { |x| a,b = splat x }
next, return value out, join using :value.
so here's quick demo whipped up:
dummy = [ ['a.txt', 'b.txt'], ['c.txt', 'd.txt'], ['e.txt', 'f.txt'], ['g.txt', 'h.txt'], ['i.txt', 'j.txt'], ['k.txt', 'l.txt'] ] threads = dummy.map |pair| thread.new(pair) { |val| val } end vals = threads.map(&:value) # equiv. 'threads.map { |t| t.value }' puts vals.inspect
crib off , should want go.
Comments
Post a Comment