function httprl_batch_callback in HTTP Parallel Request & Threading Library 7
Same name and namespace in other branches
- 6 httprl.module \httprl_batch_callback()
Given an array of data, use multiple processes to crunch it.
Similar to Map Reduce.
Parameters
$callback: The function to run
$data: The data to process.
Return value
Array of returned results.
See also
http://php.net/array-chunk#63394
1 call to httprl_batch_callback()
- httprl.examples.php in examples/
httprl.examples.php - HTTP Parallel Request Library code examples.
File
- ./
httprl.module, line 3476 - HTTP Parallel Request Library module.
Code
function httprl_batch_callback($callback, $data, $options = array()) {
// Set defaults.
$results = array();
$unified_result = NULL;
$number_of_items = count($data);
$options += array(
'max_batchsize' => 30,
'threads' => 3,
'timeout' => 120,
'multiple_helper' => FALSE,
);
// Shrink batchsize to evenly distribute workload if needed.
if ($number_of_items < $options['max_batchsize'] * $options['threads']) {
$options['max_batchsize'] = ceil($number_of_items / $options['threads']);
}
// Chunk the data.
$data = array_chunk($data, $options['max_batchsize'], TRUE);
// Convert options to httprl_queue_background_callback options.
unset($options['max_batchsize']);
$options['domain_connections'] = $options['threads'];
unset($options['threads']);
$multiple = $options['multiple_helper'];
unset($options['multiple_helper']);
// Queue up the processes.
if ($multiple) {
foreach ($data as $key => $values) {
$results[$key] =& httprl_qcinp('httprl_run_multiple', array(
$callback,
$values,
), TRUE, $options);
}
}
else {
foreach ($data as $key => $values) {
$results[$key] =& httprl_qcinp($callback, array(
$values,
), TRUE, $options);
}
}
// Execute in parallel.
httprl_send_request();
// Try to merge the results into one.
$unified = TRUE;
$is_assoc = TRUE;
foreach ($results as $key => $value) {
if (is_null($unified_result)) {
$unified_result = $results[$key];
}
elseif (is_string($results[$key]) && is_string($unified_result)) {
$unified_result .= $results[$key];
}
elseif (is_array($results[$key]) && is_array($unified_result)) {
if ($is_assoc && httprl_is_array_assoc($results[$key]) && httprl_is_array_assoc($unified_result)) {
$unified_result = httprl_lossless_assoc_array_merge($unified_result, $results[$key]);
}
else {
$is_assoc = FALSE;
$unified_result += $results[$key];
}
}
else {
$unified = FALSE;
break;
}
}
// Return results.
if ($unified) {
return $unified_result;
}
else {
return $results;
}
}