blu*_*ean 8 php asynchronous process mongodb symfony
我是Symfony2的新手,在尝试运行这样的异步命令时遇到了阻塞:
class MyCommand extends ContainerAwareCommand{
protected function configure()
{
$this
->setName('my:command')
->setDescription('My command')
->addArgument(
'country',
InputArgument::REQUIRED,
'Which country?'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$country = $input->getArgument('country');
// Obtain the doctrine manager
$dm = $this->getContainer()->get('doctrine_mongodb.odm.document_manager');
$users = $dm->getRepository('MyBundle:User')
->findBy( array('country'=>$country));
}}
Run Code Online (Sandbox Code Playgroud)
当我从命令行调用它时,这非常有效:
php app/console my:command uk
Run Code Online (Sandbox Code Playgroud)
但是当我称之为Symfony2进程时,它不起作用:
$process = new Process("php ../app/console my:command $country");
$process->start();
Run Code Online (Sandbox Code Playgroud)
我收到一个数据库错误:" [MongoWriteConcernException] 127.0.0.1:27017:not master "
我认为这意味着该过程没有得到我的数据库配置...
我只想运行异步进程,还有其他方法吗?
也许一种方法来调用不需要答案的应用程序命令继续?
也许我需要使用注射?
PS:我目前的命令只是一个测试,最后应该是一个"昂贵"的操作...
好吧,我发现发生了什么......
我使用多种环境:DEV,TEST和PROD.
我也使用不同的服务器.
所以DEV环境是我自己的机器,具有简单的mongodb配置.但TEST环境在其他服务器上具有副本集配置...
现在错误得到充分意义:"[MongoWriteConcernException] 127.0.0.1:27017:not master"
为了解决这个问题,我刚刚将环境参数(--env =)添加到流程中,一切都像魅力一样:
$process = new Process("php ../app/console my:command $country --env=test");
Run Code Online (Sandbox Code Playgroud)
实际上,为了获得正确的环境,我使用这个:
$this->get('kernel')->getEnvironment();
Run Code Online (Sandbox Code Playgroud)
让我的代码如下:
$process = new Process("php ../app/console my:command $country --env=".$this->get('kernel')->getEnvironment());
Run Code Online (Sandbox Code Playgroud)
也许这不是一个美丽的方式,但它适用于我:)
免责声明:这对于您想要做的事情来说可能有点矫枉过正:)
我会选择相反的方式来做到这一点:pthreads
首先,快速检查 StackOverflow 向我展示了一个非常好的使用示例pthreads:Multi-threading is possible in php
然后,知道您可以从另一个命令调用您的命令:
http://www.craftitonline.com/2011/06/calling-commands-within-commands-in-symfony2/
...让您可以拼凑所有零件。它有点复杂,但它可以完成工作。