From: Francis Cianfrocca Date: 2006-07-05T05:03:11+09:00 Subject: Re: Multiple Threads? ------=_Part_2638_26589455.1152043388583 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Look at EventMachine in Rubyforge. On 7/4/06, Marcus Andersson wrote: > > I need to create a little client for a third party web service. The > client should poll the service constantly. I need to monitor for new > events (they don't have callbacks apparently...) so they keep the > connection open for 15 seconds and returns nothing if nothing has > happened. If an event happens then it returns when it actually happens. > After returning I should immediately request again. > > This is fine as long as I only need to poll for one customer (since one > request only can check for one customer). The problem is that we need to > poll this service for multiple customers at the same time and then I of > course need threads. > > I was thinking along the following lines (something is wrong with the > threading parts, when I run it without threads it works so it is not the > soap stuff that is wrong) > > require 'soap/rpc/driver' > > CreateSessionResponse = Struct.new(:response_code, :session_id, :host) > > proxy = SOAP::RPC::Driver.new('url_to_service, 'urn:webservice') > [ > %w(createSession password name type tenant domain application), > %w(getEvents sessionId) > ].each do |signature| > proxy.add_method(*signature) > end > > class Client > > def initialize(name = "", password = "", type = "", tenant = "", > domain = "", application = "") > @name, @password, @type, @tenant, @domain, @application = name, > password, type, tenant, domain, application > end > > def run(proxy) > thread = Thread.new { > while true > # log in to server and get a session id > session = > CreateSessionResponse.new(*proxy.createSession(@password, @name, @type, > @tenant, @domain, @application)) > # fetch events. returns after 15 seconds if no new events > events = proxy.getEvents(session.session_id) > if events > notify_stb(events) > else > puts "no new events" # never see's this > end > end > } > thread.join # without joining the program exits immediately > end > > def notify_stb(events) > puts "Notifying client with data #{events}" # never see's this > end > > end > > puts "Starting..." > # when testing I only have data for one client, will be more later > clients = [["name", "pwd","","","domain","application"]].inject([]) { > |result, client_data| result << Client.new(*client_data)} > clients.each { |client| client.run(proxy) } > > It might be that I actually do requests. The service is remote and I > don't have a chance to check if the requests are actually getting there > (and there is also this 4:th of July in the States apparently...). > > Can anyone see anything obviously wrong? I haven't really done anything > with threads in Ruby before so... > > Regards, > > Marcus > > > > ------=_Part_2638_26589455.1152043388583--