Close connection in PHP but keep executing script

后端 未结 5 1548
孤独总比滥情好
孤独总比滥情好 2021-01-14 00:10

Anyone know how to close the connection (besides just flush()?), but keep executing some code afterwards.

I don\'t want the client to see the long proce

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 00:23

    I don't want the client to see the long process that may occur after the page is done.

    sadly, the page isn't done until after the long process has finished - hence what you ask for is impossible (to implement in the way you infer) I'm afraid.

    The key here, pointed to by Jhong's answer and inversely suggested by animusen's comment, is that the whole point of what we do with HTTP as web developers is to respond to a request as quickly as possible /end - that's it, so if you're doing anything else, then it points to some design decision that could perhaps have been a little better :)

    Typically, you take the additional task you are doing after returning the 'page' and hand it over to some other process, normally that means placing the task in a job queue and having a cli daemon or a cron job pick it up and do what's needed.

    The exact solution is specific to what you're doing, and the answer to a different (set of) questions; but for this one it comes down to: no you can't close the connection, and one would advise you look at refactoring the long running process out of that script / page.

提交回复
热议问题