PHP code example of crowdstar / background-processing
1. Go to this page and download the library: Download crowdstar/background-processing library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
crowdstar / background-processing example snippets
use CrowdStar\BackgroundProcessing\BackgroundProcessing;
$sum = 0;
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'background-processing.txt';
// First background task added.
BackgroundProcessing::add(
// Increase $sum by the sum of given numbers. Final value of $sum is this example is 7 (1+2+4).
function (int ...$params) use (&$sum) {
$sum += array_sum($params);
},
1,
2,
4
);
// Second background task added and will be executed after the first one.
BackgroundProcessing::add(
function () use (&$sum, $file) {
// Number 7 calculated from first task will be written to the file.
file_put_contents($file, $sum);
}
);
// Number 0 will be returned back to HTTP client.
echo
"Current sum value is {$sum}. ",
"Please check file {$file} in the web server; final sum value there should be 7.\n";
// Send HTTP response back to the client first, then run the two background tasks added.
BackgroundProcessing::run();
// Anything here also runs in background.
echo "This message won't shown up in HTTP response.";
// Sample code borrowed from https://github.com/symfony/demo/blob/v1.2.4/public/index.php
use App\Kernel;
use Symfony\Component\HttpFoundation\Request;
quest, $response);
// Method BackgroundProcessing::add() could be called from a bundle, a controller or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
function() {
mail('[email protected]', 'test', 'test');
}
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
// Sample code borrowed from https://github.com/laravel/laravel/blob/5.5/public/index.php
define('LARAVEL_START', microtime(true));
>handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
// Method BackgroundProcessing::add() could be called from a controller, a middleware or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
function() {
mail('[email protected]', 'test', 'test');
}
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
// Sample code borrowed from https://github.com/laravel/lumen/blob/5.5/public/index.php
$app = or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
function() {
mail('[email protected]', 'test', 'test');
}
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
// Sample code borrowed from https://github.com/slimphp/Slim/blob/3.x/example/index.php
->write("Welcome to Slim!");
return $response;
});
$app->get('/hello[/{name}]', function ($request, $response, $args) {
$response->write("Hello, " . $args['name']);
return $response;
})->setArgument('name', 'World!');
$app->run();
// Method BackgroundProcessing::add() can be called from a route, a middleware or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
function() {
mail('[email protected]', 'test', 'test');
}
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.