How to clear data in php and disconnect the user, but keep the script alive

This is a difficult question, when developing the php + ajax application, I felt some long requests, nothing wrong with them, but they could be executed in the background.

I know that there is a way to simply send a response to the user, throwing the actual processing to another process using exec (), but it does not seem to me that this is right for me, it can lead to exploits, and this does not mean it is compatible with virtual servers and cross platform.

PHP offers ob_ * functions, although they help clear the cache, but the user will continue to connect until the script is run.

I am wondering if there is an alternative to exec to support the script after sending data to the user and closing the connection / stream using apache or a less "dirty" way of processing the processing data sent to another script.

+5
source share
4 answers

After a series of studies, I found 3 answers to this question:

  • Run an ajax request with a low timeout value as soon as the timeout expires. JS will proceed to the next step, while php, in the background with it turned on ignore_user_abort, will continue processing.

  • script, ( , ), _SESSION , cURL - cron .

  • Gearman.

+1

, , Gearman.

Gearman . , , .

- $GEARMAN->doBackground("task_name","task_data");, , script.

, exec, gearman PHP.

+2
0

PHP, POSIX. , script

while(1)
{
 // run task
}

- POSIX - 120 , . , . exec() , , script 30 .

caveat - I kill the daemon and restart it every 3 hours, because sometimes it gets stuck and is no longer processed - but just killing and starting it every 3 hours is a good compromise to start live queues

edit I do not want to publish the whole script, but I can, if someone wants it

0
source

All Articles