简体   繁体   中英

symfony code after returning response

I want to return the final user a response, before I'm making all the hard processing data.

I want to simply get a request from user, return a response, it could be a simple json,

and than the user will see the data, but in my server side I would continue my rest of the processing, calling analytics, changing DB and other.

something like this idea: continue processing php after sending http response

For heavier tasks you should use the kernel.terminate Event . So any task in this event is performed, after the response was sent. This is the way, how the swiftmailer memory spool works.

This maybe a late answer, but the mentioned scenario makes a perfect use of Queues. Using a library like LeezyPheanstalkBundle will make usage of beanstalkd a lot easier.

What happens is:
1. Receive user request
2. Add job to queue (very fast)
3. Return server response
4. a worker retrieve the job and processes it.

Check out the StreamedResponse:

$response = new StreamedResponse();
$response->setCallback(function () {
    echo 'Hello World';
    flush();
    sleep(2);
    echo 'Hello World';
    flush();
});
$response->send();

Read more here: http://symfony.com/doc/current/components/http_foundation/introduction.html#streaming-a-response

While this method has its simplicity and usefulness in certain cases, it's not the best user experience in a webpage. The HTML page will only be fully loaded at the end of your script, which may cause visual annoyances and the lack of Javascript support, incomplete CSS rendering, etc...

If you're going for the best visual experience, you could load an empty page, and then make concurrent ajax calls that will do the processing. ie. each AJAX request will process 5 records and output them.

This way you can have a progress bar or some other animation going on the page...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM