PHP Symfony Silex, response is not sent before finish() -


what i'm trying this: user makes api call, give response, , start doing actions take long such sending emails. (emails take long because i'm using smtp, swiftmailer in synchronous way.)

according documentation finish() should called after response has been sent. when test however, seems not case. client side stays waiting until finish() done.

server side code:

$app = new silex\application();  $app->get('/hello', function (request $r) use($app) {      return $app->json("i should load within few milliseconds."); });   $app->finish(function() use () {     //pretend slow action afterwards     sleep(5);  });  $app->run(); 

now if load page http://localhost/hello take 5 seconds load. while expectation load instantaneous. misunderstanding documentation?

update:

technically accepted answer did asked it. practically found different solution:

i looking way emails sent in way end-user wouldn't have wait it. purposes switched on using swift_mailtransport instead of swift_smtptransport, , configured exim4 locally smarthost. way email gets handed off exim4 , script continues immediately.

if looking other long processes execute emails, have @ message queues such beanstalk, read great things them.

the accepted answer below work (also follow links in comments documentation page), found solution not clean enough particular situation.

am misunderstanding documentation?

no, don't. content of web response send before finish filters run, output not flush. try add

 ob_flush();  flush(); 

before sleep(5), , work might expect.

you may want spool emails swiftmailerbundle. see swiftmailer.use_spool.

by default, swiftmailer provider sends emails using kernelevents::terminate event, fired after response has been sent.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -