multithreading - Ruby doesn't execute the code of all threads -
i'm learning how use threads in ruby i've found strange problem. that's code:
require 'active_record' require 'rss' require 'open-uri' require 'thread' require_relative 'model/post' require_relative 'model/source' queue = queue.new producer = [] activerecord::base.establish_connection( adapter: "postgresql", host: "localhost", database: "trendy", username: "postgres", password: "postgres" ) sources = source.all puts "active record has loaded: #{sources.length} feeds" sources.each |source| producer << thread.new puts "feed: #{source.url}" end end producer.join puts "number of threads created #{producer.length}"
and that's output:
active record has loaded: 5 feeds feed: http://alt1040.com/feed feed: http://appleweblog.com/feednumber of threads created 5 process finished exit code 0
if run again, find out program doesn't print 2 feeds, mean, number of feeds program prints random.
i can't see problem...
you're not waiting them complete. need call producer.each(&:join). producer.join calling join on array
Comments
Post a Comment