Reputation: 1
I was wondering. If you have a PHP request and the PHP code calls a command line tool via exec
..
Is the command line then processed in another thread than the one the PHP script is running under?
And if so are both shared over more than one core?
Upvotes: 0
Views: 503
Reputation: 14230
As the manual suggests:
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
This might give you an idea that the process is not forked and runs on the main thread. PHP actually waits for the output to end and get the last line of the log.
However, there is a way to run your command in background:
nohup MY_COMMAND > /dev/null 2>&1 & echo $!
Since exec
receives the value of the last line of the output, this will return the pid
of the process and you will be able to kill
it later on.
fastcgi_finish_request()
function if you use fpm - FastCGI Process Manager.
That function gives the client an answer without stopping the script execution. For instance, this is how you send an e-mail without fpm
:
mail($recipient, 'Subject', 'Hello, Kitty!');
echo 'Okay, we sent the e-mail';
And this is how you may send an email with fpm
:
echo 'Okay, we sent the e-mail';
fastcgi_finish_request();
mail($recipient, 'Subject', 'Hello, Kitty!');
In this case the client will get the echoed text and the mail
function will still run in the background.
Just do not forget to close your sessions with session_write_close();
before you use fpm
;)
Upvotes: 1