0

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?

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

3 Answers3

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