From: Dan Zwell Date: 2007-04-06T12:19:58+09:00 Subject: Re: newbie, hash or 2dArry + threads I admit I didn't fully read the whole script, but I saw something that looks wrong. A simple syntax mistake? See further down. Zac Elston wrote: > first, thanks for the responses, ruby really is a great language. > > My delima is that I'm trying to multithread the actions and store the > results in a hash with the hostname as key > > to redefine the issue, for each host I need a thread and I expect a > result, then I'd like to be able to see the result in a > hash[host][result] > > but I'm having trouble mixing the hash with the thread. maybe I'm going > about this all wrong. (this is outputting to a rails view, which is why > i have "@vars" > > I have > > @threads = [] > @resulthash = Hash.new(0) > @hostarray.each do |host| > threads << Thread.new(host) do |myhost| Here, you are resetting @resulthash. Maybe you meant: @resulthash[myhost] = {:packages => @mypackages, :result => doXMLquery(host,@mypackages)} > @resulthash = { :myhost => { > :packages => @mypackages, > :result => doXMLquery(host,@mypackages)} } > logger.info("host = " + host + ", result = " + > @resulthash[:myhost][:result]) > end > end > > @threads.each {|thr| thr.join } > > logger.info("result of threads..") > @hostarray.each do |myhost| > logger.info("host = " + myhost + ", result = " + > @resulthash[:myhost][:result]) > end > > logger output > > host = hostX, result = --data returned from hostX-- > host = hostY, result = --data returned from hostY-- > result of threads.. > host = hostX, result = --data returned from hostY-- > host = hostY, result = --data returned from hostY-- > > I'm clearly loosing context of the [:host][:result] in the final two > lines. > > any pointers? > > thanks > -zaq >