How to finish http response and do further process

2019-07-21 16:24发布

问题:

In my case I need to echo a flag to client side and send an email .

Now client side needs to wait until the email is sent...

But I want to separate these two steps,how to do that?

回答1:

You could take a look at Run PHP Task Asynchronously which is pretty much what you want to accomplish.



回答2:

You could take a look at Gearman

Gearman is a system to farm out work to other machines, dispatching function calls to machines that are better suited to do work, to do work in parallel, to load balance lots of function calls, or to call functions between languages.



回答3:

Have another php file to send emails, and call it with some parameters using shell_exec . You can also call the URL on command line using CURL with some parameters.

That would work fine, you can track your email success status in the target file.

pseudo code:

my main file :

php stuf...
shell_exec("usr/bin/php  mySecondfile.php someParam  > /dev/null 2>/dev/null &");
other stuff continued.
send SUCCESS


回答4:

You can use pcntl_fork() function for that. With pcntl you can fork processes to child process with different pid.

http://php.net/manual/en/function.pcntl-fork.php



标签: php http