Reputation: 761
My page executes a script that takes a relatively long time to complete. I would like to make it so that the user can submit information, immediately echo "Complete", and allow the user to exit the page while the script continues executing. How can I do this?
Upvotes: 2
Views: 250
Reputation: 95101
Email is naturally slow. I would advise you to use a job queue for your emails. You should look at
With these solutions, you can queue slow tasks and continue to show valid information and progress to your user.
Example Client
$client= new GearmanClient();
$client->addServer();
$client->do("email", json_encode(array("[email protected]","Hello World","This is your first Mail")));
echo "Welcome To XYZ" ;
Server
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("email", "sendMail");
while ( $worker->work() );
function sendMail($mail) {
list($to, $subject, $message) = json_decode($mail);
return mail($to, $subject, $message);
}
Upvotes: 2
Reputation: 10084
My suggestion would be to submit all of the information into a table row or similar data structure, then run a cronjob every few minutes to go through each row and run the script based on the information that had been submitted.
This would be slightly complicated, I'm afraid, but it would immediately free the user (once the raw information was stored into the DB)
Upvotes: 1
Reputation: 11080
Use cron. On your page create a email task and save in in db or fs - does not matter. Create a script which runs every n minutes which gets email tasks and executes them.
Unfortunately, your hosting may not have cron support...
Upvotes: 2