Commit 4ef702aa authored by mikeytown2's avatar mikeytown2

Issue #1427958 by mikeytown2: add in documentation for threading.

parent e651a227
------------------------------------
HTTP PARALLEL REQUEST LIBRARY MODULE
------------------------------------
------------------------------------------------
HTTP PARALLEL REQUEST & THREADING LIBRARY MODULE
------------------------------------------------
CONTENTS OF THIS FILE
......@@ -12,6 +12,7 @@ CONTENTS OF THIS FILE
* Configuration
* API Overview
* Technical Details
* Code Examples
ABOUT HTTPRL
......@@ -64,6 +65,10 @@ httprl_request()
httprl_send_request()
- Perform many HTTP requests.
Create and use a thread:
httprl_queue_background_callback()
- Queue a special HTTP request (used for threading) in httprl_send_request().
Other Functions:
httprl_background_processing()
- Output text, close connection, continue processing in the background.
......@@ -73,6 +78,10 @@ httprl_glue_url()
- Alt to http_build_url().
httprl_get_server_schema()
- Return the server schema (http or https).
httprl_pr()
- Pretty print data.
httprl_fast403()
- Issue a 403 and exit.
TECHNICAL DETAILS
-----------------
......@@ -83,3 +92,176 @@ the http response; Non-Blocking will close the connection not waiting for the
response back. The API for httprl is similar to the Drupal 7 version of
drupal_http_request().
CODE EXAMPLES
-------------
Request this servers own front page & the node page.
<?php
// Build URL to point to front page of this server.
$url_front = httprl_build_url_self();
$url_node = httprl_build_url_self('node');
// Queue up the requests.
httprl_request($url_front);
httprl_request($url_node);
// Execute requests.
$request = httprl_send_request();
// Echo out the results.
echo httprl_pr($request);
?>
Request 10 URLs in a non blocking manner on this server. Checkout watchdog as
this should generate 10 404s and the $request object won't contain much info.
<?php
// Set the blocking mode.
$options = array(
'blocking' => FALSE,
);
// Queue up the requests.
$max = 10;
for ($i=1; $i <= $max; $i++) {
// Build URL to a page that doesn't exist.
$url = httprl_build_url_self('asdf-asdf-asdf-' . $i);
httprl_request($url, $options);
}
// Execute requests.
$request = httprl_send_request();
// Echo out the results.
echo httprl_pr($request);
?>
print 'My Text'; cut the connection by sending the data over the wire and do
processing in the background.
<?php
httprl_background_processing('My Text');
// Everything after this point does not affect page load time.
?>
Use a callback in the event loop to do processing on the request. In this case
we are going to use httprl_pr() as the callback function.
<?php
// Setup return variable.
$x = '';
// Setup options array.
$options = array(
'method' => 'HEAD',
'callback' => array(
array(
'function' => 'httprl_pr',
'return' => &$x,
),
),
);
// Build URL to point to front page of this server.
$url_front = httprl_build_url_self();
// Queue up the request.
httprl_request($url_front, $options);
// Execute request.
$request = httprl_send_request();
// Echo returned value from function callback.
echo $x;
?>
Use a background callback in the event loop to do processing on the request.
In this case we are going to use httprl_pr() as the callback function. A
background callback creates a new thread to run this function in.
<?php
// Setup return variable.
$x = '';
// Setup options array.
$options = array(
'method' => 'HEAD',
'background_callback' => array(
array(
'function' => 'httprl_pr',
'return' => &$x,
),
),
);
// Build URL to point to front page of this server.
$url_front = httprl_build_url_self();
// Queue up the request.
httprl_request($url_front, $options);
// Execute request.
$request = httprl_send_request();
// Echo returned value from function callback.
echo $x;
?>
Use a background callback in the event loop to do processing on the request.
In this case we are going to use print_r() as the callback function. A
background callback creates a new thread to run this function in. The first
argument passed in is the request object, the FALSE tells print_r to echo out
instead of returning a value.
<?php
// Setup return & print variable.
$x = '';
$y = '';
// Setup options array.
$options = array(
'method' => 'HEAD',
'background_callback' => array(
array(
'function' => 'print_r',
'return' => &$x,
'printed' => &$y,
),
FALSE,
),
);
// Build URL to point to front page of this server.
$url_front = httprl_build_url_self();
// Queue up the request.
httprl_request($url_front, $options);
// Execute request.
$request = httprl_send_request();
// Echo what was returned and printed from function callback.
echo $x . "<br />\n";
echo $y;
?>
Use 2 threads to load up 4 different nodes.
<?php
// List of nodes to load; 241-244.
$nodes = array(241 => '', 242 => '', 243 => '', 244 => '');
foreach ($nodes as $nid => &$node) {
// Setup callback options array.
$callback_options = array(
array(
'function' => 'node_load',
'return' => &$node,
// Setup options array.
'options' => array(
'domain_connections' => 2, // Only use 2 threads for this request.
),
),
$nid,
);
// Queue up the request.
httprl_queue_background_callback($callback_options);
}
// Execute request.
httprl_send_request();
// Echo what was returned.
echo httprl_pr($nodes);
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment