小编blu*_*ean的帖子

异步子进程中的Symfony2命令

我是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:我目前的命令只是一个测试,最后应该是一个"昂贵"的操作...

php asynchronous process mongodb symfony

8
推荐指数
2
解决办法
3105
查看次数

如何在PHP + Doctrine中执行巨大循环?

我正在尝试使用Symfony2(MongoDB + Doctrine)在PHP中开发一个简单的“ Ranking”脚本。

    $users = $dm->createQueryBuilder('Account')
                ->sort('points', 'DESC')
                ->getQuery()
                ->execute();

    foreach($users as $user){
            // Do nothing
    }
Run Code Online (Sandbox Code Playgroud)

我遇到以下问题:

PHP致命错误:耗尽了134217728字节的允许的内存大小(尝试分配25个字节)

我的数据库有80.000个用户,因此$ users拥有所有用户。

好吧,我知道我可以修改php config以允许更多的内存。我要去做。但是我正在寻找其他类型的解决方案(向PHP配置中添加额外的内存)。

我的想法是我尝试使用“分页”来开发它,但是它也不起作用(我在用户272处收到错误,第3次迭代):

    $limit = 100;
    $skip = 0;

    $total = $dm->createQueryBuilder('Account')->count()->getQuery()->execute();
    $max = $total/$limit;

    while($skip<=$max){

        $users = $dm->createQueryBuilder('Account')
            ->sort('points', 'DESC')
            ->limit($limit)
            ->skip($skip*$limit) // 0x100, 1x100, 2x100, 3x100
            ->getQuery()
            ->execute();

        foreach($users as $user){
               // Do nothing
        }

        $skip++;

    }
Run Code Online (Sandbox Code Playgroud)

你知道我该怎么办吗?

PS:我认为我已经完成了本文中的建议:如何使用symfony和doctrine处理庞大的select查询?

php performance symfony doctrine-orm

1
推荐指数
1
解决办法
2865
查看次数