Requests: Parallel Processing

From Nephtali Documentation

Jump to: navigation, search

Web services are of core importance in any modern web application. Many websites are completely driven by interaction with web services (i.e., no direct contact with a DB), and as services such as Amazon's SimpleDB and Rackspace's CloudFiles continue to gain traction, this trend will likely become even stronger.

Parallel Web Requests

Nephtali provides a simple means to improve the performance of pages that rely on multiple web services. Leveraging the parallel processing capabilities of CURL, Nephtali allows you to register requests just as you would pipes or ports. Any registered requests are processed in parallel. The results can then be retrieved for use in any pipe.

The code below is used to render the Nephtali homepage, which makes use of 2 separate web services (Google Code and the Wordpress RSS feed.)

n\curl\request\register($url = 'http://code.google.com/feeds/p/nephtali/downloads/basic');
n\curl\request\register($url = 'http://blog.nephtaliproject.com/?feed=rss2');

n\pipe\register(
	$name = 'downloads', 
	$function = function($markup)
	{
		if (count($rows = n\xml\parse(
			$xml_str = n\curl\response\get('http://code.google.com/feeds/p/nephtali/downloads/basic'), 
			$result = array(), 
			$xpath_functions = array(
				'/feed/entry' => function($node, $result)
				{
					$row['link'] = (string)$node->link['href'];
					$row['title'] = (string)$node->title;
					$result[] = $row;
					return $result;
				}
			)
		)))
		{
			return n\view\render($name = 'default', $markup, $rows);
		}

		return n\view\render($name = 'empty', $markup);
	},
	$opts = array('auto_error_handling' => false)
);

n\pipe\register(
	$name = 'announcements', 
	$function = function($markup)
	{
		if (count($rows = n\xml\parse(
			$xml_str = n\curl\response\get('http://blog.nephtaliproject.com/?feed=rss2'), 
			$result = array(), 
			$xpath_functions = array(
				'/rss/channel/item' => function($node, $result)
				{
					$row['description'] = (string)$node->description;
					$row['link'] = (string)$node->link;
					$row['title'] = (string)$node->title;
					$result[] = $row;
					return $result;
				}
			)
		)))
		{
			return n\view\render($name = 'default', $markup, $rows, $opts = array('whitelists' => array('description' => array('’','>')), 'parallel' => true));
		}

		return n\view\render($name = 'empty', $markup);
	}
);

HTMLFRAG Requests

Because htmlfrag requests typically retrieve one pipe, Nephtali waits for the call to retrieve the response and does not perform parallel processing to improve performance. For example, in the code above, only the Wordpress feed would be processed in an htmlfrag request for the announcements pipe.

However, you can quite easily jump into the code and handle the parallel processing directly if you have a complicated pipe that needs to have parallel processing completed during htmlfrag requests, too.

Personal tools