Reputation: 9431
I have to do a lot of processing in the server end, but don't want user to aware of it. I want to send user a response immediately and then run the process in the server end.
I've done some study, and so far I've done this:
function process(){
ignore_user_abort(true);
set_time_limit(0);
ob_start();
// Send response to the user
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
// Post process
}
As far as I'm aware of, this should return the user response immediately and then run the post processes. But it's not working: it still waits until all the processes are done.
Bytheway, I'm sending an AJAX request if it helps. At first I thought it might be a problem with session locking, but now I'm not even using any session (session_status()
returns 1
).
PHP version is 7.1
NOTE: I've read other related questions where the above method works fine, but for some reason it's not working in my case.
Upvotes: 1
Views: 1680
Reputation: 9431
It looks like (as @ADyson said in the comment) flushing output doesn't seem work with AJAX. So, the only solution to the problem is to use Cron Job. To do this, I used PHP Cron Scheduler. But crontab can be used directly to add a Cron Job to the list.
I added the post processes to another PHP file with suitable changes so that it can do the same tasks from the script and added the file to the Scheduler.
This solution may seem a real pain, but there is no solution to this problem other than using Cron Job.
Upvotes: 1