I have a PHP application which receives data from the user. I would like the script to send a quick response, close the connection with the browser and then process the data. My main concern is that as the data processing takes quite a while and there are many concurrent users on the website, I will soon get to a point where there are too many open connections. Any ides on how I could do this?
Asked
Active
Viewed 1,000 times
0
-
1Check the answer here: http://stackoverflow.com/a/4856241/108574 . This is a duplicate question. – He Shiming Mar 24 '12 at 17:12
-
You are right He Siming, I got to the same answer via another way and indicated this solution here below – Adrien Hingert Mar 26 '12 at 00:08
3 Answers
0
Submit the user's data to a database. Have a cronjob take care of the processing. If the user needs to see the result afterwards, use polling with Ajax to wait until the data is processed. A cronjob is a command that gets executed every set period of time on a server. You can make it execute a PHP script that does the processing every minute or whatever you need.

John Kurlak
- 6,594
- 7
- 43
- 59
0
I think gearman would be a good solution.
As you say, running out of resources, because of running to many processes is the big issue, this is one of the things a queuing system like gearman handles. So what ever you do, you need to queue the work.

Jacob Oettinger
- 766
- 3
- 7
0
I think I've found the solution I needed. Here following is the PHP code I'm using. After this code I can keep processing as I need:
// http://www.php.net/manual/en/features.connection-handling.php#71172
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
echo $user_message;
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush();

Adrien Hingert
- 1,416
- 5
- 26
- 51