From: Tony Arcieri Date: 2008-02-13T07:30:33+09:00 Subject: Re: Rev/actor TCP monkey patching ------=_Part_2634_19078877.1202855438785 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Feb 12, 2008 1:38 PM, fedzor wrote: > On a side note, would you be able to post some more example with > Actors to achieve concurrency? Because at first I understand your > echo server, and then I tested out actors myself and everything I > knew fell apart.... > Here's a simple example. It will create a new Actor for each specified URL, which probably isn't the behavior you want. You'll instead want something like a queue. Also, this will gib if you have duplicates in the URL list. And as another bonus, I haven't tested it, but I think it will work :) However, it should be enough to get you started: require 'revactor' # A list of URLs we want to fetch url_list = ['http://www.google.com', ...] # Capture the parent Actor to send messages to parent = Actor.current # Spawn a new Actor for each URL we want to fetch url_list.each do |u| Actor.spawn do # Request the URL response = Revactor::HttpClient.get(u) # Consume and store the body if the response status was 200 response.body if response.status == 200 # Close the connection response.close parent << [:http_response, u, response] end end # Store all the responses in a hash mapping URLs to the responses responses = {} # Consume all the responses while responses.size < url_list.size Actor.receive do |filter| # Catch messages which start with :http_response and contain two objects filter.when(Case[:http_response, Object, Object]) do |_, url, response| responses[url] = response end end end # Inspect the responses p responses -- Tony Arcieri ClickCaster, Inc. tony@clickcaster.com ------=_Part_2634_19078877.1202855438785--