From: Francis Cianfrocca Date: 2007-02-23T01:23:16+09:00 Subject: Re: threads within a thread ------=_Part_32625_8855982.1172161389711 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 2/22/07, Robert Klemme wrote: > > I would rather use EventMachine (as mentioned, would be a great > opportunity to learn it) or a fixed number of threads like in a typical > farmer worker scenario. Basically you create N threads and feed them > tasks via a queue. A task would be to check one port on one host. The > advantage is that you can control concurrency and find out the optimal > level. Another advantage is that you save the overhead of multiple > thread creations and destructions. A quick, naive EM implementation of a part of this problem would be something like the following. (NB, there are no threads being spun anywhere here). #----------------------------------------------------- require 'rubygems' require 'eventmachine' ip_stem = "1.2.3." start = Time.now EventMachine.run { total = 0 256.times {|n| [80,443,22].each {|port| ip = "#{ip_stem}#{n}" a = EventMachine::Protocols::TcpConnectTester.test( ip, port ) count = proc { total += 1 EventMachine.stop if total == 256 * 3 } a.callback { $>.puts "Connected #{ip}, port #{port}" count.call } a.errback { count.call } } } } $>.puts "Time elapsed: #{Time.now - start}" #----------------------------------------------------- Tunings and extras not included, YMMV. ------=_Part_32625_8855982.1172161389711--