symfony3长期执行时间

时间:2017-07-12 14:45:34

标签: php symfony

我有文件上传监听器,它上传文件,解析它并通过Doctrine将数据放入数据库。脚本如下:

<?php

namespace AppBundle\EventListener;
use CommonBundle\Entity\Classifiers;
use Doctrine\Common\Persistence\ObjectManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use CommonBundle\Entity\ClassifierPhrase;
use CommonBundle\Entity\ClassifierSubject;

class UploadListener
{
/**
 * @var ObjectManager
 */
private $om;

public function __construct(ObjectManager $om)
{
    $this->om = $om;
}

public function onUpload(PostPersistEvent $event)
{
    $type = $event->getRequest()->get('type');
    if($event->getRequest()->get('classifier_id')){
        $classifier_id = $event->getRequest()->get('classifier_id');
        $classifier = $this->om->getRepository('CommonBundle:Classifiers')->findOneBy(['id' => $classifier_id]);
    }

    $file = $event->getFile();
    $contents = file_get_contents($file->getPathname());
    $entries = preg_split("/\\r\\n|\\r|\\n/", $contents);

        foreach($entries as $entry) {
            $time_start = microtime(true); //start here
            //some parsing logic
            $this->om->persist($phrase);
            $this->om->flush();
            $time_end = microtime(true);
            $execution_time = round(($time_end - $time_start)/60, 4);
            file_put_contents('C:\xampp\htdocs\perfomance.txt', 'time: ' . $execution_time. ' phrase: '.$entry[0].PHP_EOL, FILE_APPEND);
//takes about 0.005 sec per entry
        }
    $response = [$event->getResponse(), 'success' => true];
    return $response;
  }
}

因此,在加价时,大约需要0.005秒才能完成工作。我在我的文件和php max_execution指令中有5000个条目300秒,它不能处理所有条目5分钟。同样在脚本执行期间,我看到如何将条目添加到我的日志文件中,并且我看到它正在通过条目添加条目,但速度很慢(每秒大约20个条目)。所以它在我的foreach循环中解析条目期间等待某些事情。任何想法如何描述脚本及其为何如此持久的想法都会受到欢迎。谢谢。

1 个答案:

答案 0 :(得分:0)

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/batch-processing.html

  

ORM工具主要不适合大规模插入,更新或删除。每个RDBMS都有自己的,最有效的方式来处理此类操作,如果下面列出的选项不足以满足您的需要,我们建议您使用适用于这些批量操作的特定RDBMS工具。

这意味着您不应将Doctrine ORM用于批量插入。就那么简单。 请改用Doctrine DBAL和SQL。

相关问题