0

First, I am not looking for ignore_user_abort. Instead, I look for a function to finalize the output and send the document to the user. After that, I'd like the php script to do additional process which takes another seconds without effects for the user, only background process.

There is no reason for the user to wait for this to be completed, too. The output is ready for catch up before already. Does PHP provide a feature for closing the connection to the user, but remain for a while on the server to complete the work?

ledy
  • 1,527
  • 6
  • 22
  • 33

5 Answers5

1

One of the best features of php-fpm You is fastcgi_finish_requests(). This will finish the session for the client side and continue the process at serverside. It's also best use-case for messaging as an alternative for queque or delayed processing by cron.

However, it's restricted to php-fpm!

rabby
  • 66
  • 2
0

You should split your work into 2 files, (The one with the initial processing, which produces output), and the second, heavier one which takes several more seconds but should not make the user to wait until its completion.

Then, have the browser call it with either Ajax or a hidden iframe.

You could even have the second script return a success/fail notice and catch that with JavaScript to notify the user, without affecting his actual initial output.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • This may be a solution, but since i run this on an adserver, the load will be massive and expensive, even if the 2nd file's output is empty, it means 2 requests (and worker process) per real request (file1). If my server can handle 3000 calls per second currently, this additional traffic is reducing the performance around -50% :-( OTher solutions without additional calls? – ledy Jun 29 '12 at 07:20
  • There's another option that's implementation specific. With PHP-FPM you can use `fastcgi_finish_request()` to flush and close the connection, while allowing processing to continue. – Leigh Jun 29 '12 at 12:39
0

looks like what you want is some ajax stuff. if you send the response back to the user, the script will end. but using javascript you can start an ajax call to another php script that will not change anything for the user.

Antoine
  • 67
  • 7
0

you could try using curl(); to ping localhost and trigger another script execution to run any extra tasks you have.

mikegreiling
  • 1,160
  • 12
  • 21
0

Once the script has been run, add an entry into a queue table. Then run a cron job to process the queue, locking the record and then deleting them as they get processed, so if your crons overlap they don't do the same job twice.

Waygood
  • 2,657
  • 2
  • 15
  • 16
  • i.e. it can not be initiated _instantly_ when the output is sent? all data is in cache at the final line when the output is sent. when loading by cron or slightly later, all information must be read or load again before finalizing the last step. – ledy Jun 29 '12 at 11:35