Is it possible to execute some background command and without waiting for it completes, return response?(Symfony2) example:user submit form and gets response, but in background executes some external calculations.
是否可以执行一些后台命令而无需等待它完成,返回响应?(Symfony2)示例:用户提交表单并获得响应,但在后台执行一些外部计算。
The most correct way in my opinion is to use RabbitMQ. You can send message to Rabbit and with an cron job execute needed command. Something like that: http://cases.azoft.com/using-rabbitmq-in-symfony2-projects/
我认为最正确的方法是使用RabbitMQ。您可以向Rabbit发送消息并使用cron作业执行所需的命令。类似的东西:http://cases.azoft.com/using-rabbitmq-in-symfony2-projects/
Just refer this documentation
请参阅此文档
You can run the command in the background by adding a "&
" at the end of it as:
您可以在后台运行命令,在其末尾添加“&”,如下所示:
exec('Your Command &');
Usually you would put the commands in a shell script and call that from exec, however the above trick will help where the sequence is dynamically generated from php.
通常你会把命令放在shell脚本中并从exec调用它,但是上面的技巧将有助于从php动态生成序列。
NOTE:- If a program is started with exec 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.
注意: - 如果程序是使用exec函数启动的,为了使程序继续在后台运行,程序的输出必须重定向到文件或其他输出流。如果不这样做将导致PHP挂起,直到程序执行结束。
So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/null if you want to discard it as:
因此,如果要稍后查看,可以将命令的stdout重定向到文件,如果要将其丢弃,则可以将其重定向到/ dev / null:
exec('Command > /dev/null &');
For a sequence of commands, enclose them within parentheses then append the & symbol but be sure to redirect stdout, stderr somewhere otherwise your script will hang waiting e.g.:
对于一系列命令,将它们括在括号内,然后附加&符号,但一定要重定向stdout,stderr,否则你的脚本会挂起等待,例如:
<?php
exec('( sleep 10; echo "finished" | mail ian@example.com ) &> /dev/null &');
?>
Thanks!
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/05/02/aecd318209fe42acf5ec5cebf14adf3b.html。