0

Is it possible in PHP, to send the data right now to the client, and continue the PHP processing (that might be blocking) ?

<?php

// some code 

echo json_encode(array('ok' => '1'));  // the client is waiting for this AJAX answer !!
// how to send the response right now before finishing this PHP file ?

// the output is REALLY finished here, so client, you can work with it

some_blocking_processing();  // this is just some server processing that would
                             // block the client for ~ 5 seconds
                             // but it produces no output useful for client

?>

I know the right way might be to use queues or other process to perform the processing.

But just as a general purpose question, is it possible to send the data to the client, in the middle of a PHP file?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Yes, look into [output buffering](http://php.net/manual/en/book.outcontrol.php) – John Conde Mar 30 '15 at 22:18
  • @JohnConde Just `ob_start()` at the beginning and `ob_flush()` after `echo ...` and that's all ? – Basj Mar 30 '15 at 22:25
  • At its simplest, yes. It may require more login depending on exactly what you're doing but that's the basic gist of it. – John Conde Mar 30 '15 at 22:26
  • I tried various combinations of `ob_flush`, `ob_start`, `ob_*` etc. but none of them worked. Probably the client is waiting for the END of the AJAX answer before doing something... Is there a way to tell "this is the end of the output. You, client, can start dealing with it." ? – Basj Mar 30 '15 at 22:49

1 Answers1

1

Well it really depends on what some_blocking_processing() is actually doing. I can't come up with a solution without knowing what is happening there.

However I can point you to some areas where you can do more research. One of these might be the right solution for you:

  1. PHP threading
  2. spawning asynchronous php process
  3. logging your state in file/db and then do the extra processing via a cron job
Community
  • 1
  • 1
A squared
  • 131
  • 6
  • `some_blocking_processing()` does download a .JPG from internet (not uploaded from the user...) and converts it into a thumbnail. If the image is big (for ex. `5000x10000px`), it might block for 2 ~ 5 seconds. – Basj Mar 31 '15 at 00:05
  • Here it is @Asquared : http://pastebin.com/aHCRwUtZ – Basj Mar 31 '15 at 00:13
  • why can't you do that in a cron job? just save the filename from your article into a queue (text file on the server or database) and then have a cron job to download and create the images. this file will run completely independent of your php code so not impacting the speed. – A squared Mar 31 '15 at 00:41
  • Probably @Asquared, but I really don't know currently how to do that. And moreover my thumbnail-generation is now written in PHP. I don't want to move it to another language. If you know how to do, can you provide a solution with a few lines of code to show how it would work? – Basj Mar 31 '15 at 06:39
  • :) just Google cron job. It's not a language. It is not that hard and you really need to learn it to be a php developer. All u need to do is to copy your image generation php code to a new file and run it via a cron job. – A squared Mar 31 '15 at 07:34