• Learn more:
  • Overview
  • Features
  • Buzz
  • Tutorials & Documentation
  • Discussion
  • Contribute
  • Blog

WarpShare CloudFusion

Managing multi-threaded requests (i.e. MultiCurl, curl_multi_exec)

Audience

This tutorial is for people who want to further optimize and speed up their CloudFusion-based web applications by executing certain methods in parallel.

Getting started

CloudFusion leverages a class called RequestCore under the hood to manage all of the HTTP requests and responses. RequestCore is built on top of PHP's cURL extension, and therefore has access to the curl_multi_exec() function.

Every CloudFusion method that is capable of making a request to the AWS service has the option of setting a special setting called returnCurlHandle. If you set this setting to true, then instead of executing the HTTP request, CloudFusion will return something called the cURL handle. You can collect the cURL handles of multiple requests, and fire them all at once with the send_multi_request() method.

<?php
require_once 'cloudfusion.class.php';

// Create an array to hold the cURL handles
$handles = array();

/************************************************************/

// Collect cURL handles
$s3 = new AmazonS3();
$handles[] = $s3->list_buckets(true); // set returnCurlHandle to true

$sdb = new AmazonSDB();
$handles[] = $sdb->list_domains(array(
	'returnCurlHandle' => true // set returnCurlHandle to true
));

$sqs = new AmazonSQS();
$handles[] = $sqs->list_queues(null, true); // set returnCurlHandle to true

/************************************************************/

// Instantiate a new RequestCore object
$http = new RequestCore();

// Fire all of the handles we collected in parallel.
// Returns an indexed array of all responses.
$all_responses = $http->send_multi_request($handles);

// Loop through the array of responses
foreach ($all_responses as $response)
{
	// Parse the response bodies with SimpleXML
	$response->body = new SimpleXMLElement($response->body);
}

/************************************************************/

// View the indexed array of responses
header('Content-type: text/plain; charset=utf-8');
print_r($all_responses);

?>

What's next?

If you're building a web application, you'll want to take a look at your application flow and determine which requests can be run in parallel. For example:

  1. You fetch a list of objects stored in an S3 bucket (e.g. 200 results).
  2. You want to determine the ACL settings for each item.
  3. You loop through the list of objects and store the cURL handles from get_object_acl().
  4. You fire off the requests in parallel using the aforementioned method.
  5. You iterate over the results and determine the ACL settings.

Firing off linear requests for 200 objects (for example) at 300ms per request would take 60 seconds (200 x 300ms). However, firing them off in parallel only takes as long as the slowest request (e.g. 300ms).

Add in caching (another topic for another tutorial), and you could reduce the load time (for subsequent requests) by nearly 10x (e.g. 30ms). Properly using parallel requests and caching can substantially improve your application performance.


Skill Level
Intermediate
Tools you need
  • Tarzan 2.0 or greater (including CloudFusion)
More information
  • More Tutorials
  • curl_multi_exec()
  • SimpleXML
Software Listings
  • AWS Solutions
  • Ohloh
  • HotScripts
  • FreshMeat
Development
  • Github
  • Bug Tracker
  • RSS Notifications
  • API Reference
Be Social
  • Blog
  • Twitter
  • Facebook
  • LinkedIn
Legal Mumbo-Jumbo

CloudFusion™ is © 2007–2010 Ryan Parman, Foleeo Inc., and contributors. The CloudFusion name and logo are trademarks of Ryan Parman. WarpShare™ is a trademark of Foleeo Inc. The source code is provided under the New BSD License. Documentation and tutorial text is provided under the [CC] BY-NC-SA license. All other rights are reserved.