Consider an application which require lacs of data to be processed, which should be processed in the background parallely and you don't have to wait for the process to finish the task and you have to show progress/failure of the processing task.
Let me explain how it could be acheived with gearman and PHP.
Gearman provides an application framework to give out work to other machines/processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing.
A Gearman powered application consists of three parts: a client, a worker, and a job server. The client is responsible for creating a job to be run and sending it to a job server. The job server will find a suitable worker that can run the job and forwards the job on.
you can read more about gearman by following link below
http://gearman.org/
It has php extension. You can read more about the same by following link below.
http://php.net/manual/en/book.gearman.php
you can create multiple worker which will run the jobs in parallel. you can submit data to the functions defined in the workers, to process and accomplish the task faster in background mode, so that you don't have to wait till process is finished.
creating multiple workers means running multiple instance of the worker script. where client script will submit the jobs to be process.
sample client code
// ... handle form validation, etc
$email = 'joe@hamburger.com';
$subject = "Eat @ Joe's!";
$body = 'They have the best ceaser salad!';
// ... now the good stuff
$client = new GearmanClient();
$client->addServer();
$result = $client->doBackground("send_email", json_encode(array(
// whatever details you gathered from the form
'email' => $email,
'subject' => $subject,
'body' => $body
)));
.
.
?>
Sample worker code
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("send_email", "sendmail");
function sendmail(GearmanJob $job) {
$workload = json_decode($job->workload());
echo "Sending email: " . print_r($workload,1);
// You would then, of course, actually call this:
//mail($workload->email, $workload->subject, $workload->body);
}
while ($worker->work());
.
.
.
?>
No comments:
Post a Comment