diff --git a/README.txt b/README.txt index a1d8c2b..f574095 100644 --- a/README.txt +++ b/README.txt @@ -1,7 +1,7 @@ ------------------------------------- -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. + + + + +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. + + 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. + + + + +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. + + '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. + + '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. + + '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 . "
\n"; + echo $y; + ?> + + +Use 2 threads to load up 4 different nodes. + + '', 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); + ?> +