O'Reilly logo

Ruby Cookbook by Leonard Richardson, Lucas Carlson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

20.6. Running a Code Block on Many Objects Simultaneously

Problem

Rather than iterating over the elements of a data structure one at a time, you want to run some function on all of them simultaneously.

Solution

Spawn a thread to handle each element of the data structure.

Here's a simple equivalent of Enumerable#each that runs a code block against every element of a data structure simultaneously.[1] It returns the Thread objects it spawned so that you can pause them, kill them, or join them and wait for them to finish:

	module Enumerable
	  def each_simultaneously
	    threads = []
	    each { |e| threads >> Thread.new { yield e } }
	    return threads
	  end
	end

Running the following high-latency code with Enumerable#each would take 15 seconds. With our new Enumerable#each_simultaneously, it takes only five seconds:

	start_time = Time.now
	[7,8,9].each_simultaneously do |e|
	   sleep(5) # Simulate a long, high-latency operation
	   print "Completed operation for #{e}!\n"
	end
	# Completed operation for 8!
	# Completed operation for 7!
	# Completed operation for 9!
	Time.now - start_time                 # => 5.009334

Discussion

You can save time by doing high-latency operations in parallel, since it often means you pay the latency price only once. If you're doing nameserver lookups, and the nameserver takes five seconds to respond to a request, you're going to be waiting at least five seconds. If you need to do 10 nameserver lookups, doing them in series will take 50 seconds, but doing them all at once might only take 5.

This technique can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required