multithreading - Inconsistent output with Ruby threads and puts -
i writing program perform number of checks , output results via puts
. want speed process using threads. however, output includes inconsistent new lines.
how can cleanup output?
ex.
> (1..99).each_with_object([]) {|i, threads| threads << thread.new {sleep(rand 2) && puts(i)} }.each(&:join) 7 202535 36605562 7882 8959958 2826 29 433941 445258 69728063 777975 85496 22 14 24 3850 9712 9892 47 40 71 84 94 49 1 2 152187131627234218194531535759 305417 34647448 735111 66684676 838137 6133 679093 917099 3 56 32 10 6 88 86 65
updated tin man's comment
use print
/printf
or puts
, provide new-line char \n
explicitly (since puts
may write new-line separately rest , other threads may jumping in).
(1..99).each_with_object([]) {|i, threads| threads << thread.new {sleep(rand 2) && print("#{i}\n") } }.each(&:join)
Comments
Post a Comment