具有多个线程持久和刷新的Doctrine实体管理器

时间:2022-10-22 18:45:57
class Job extends \Stackable
{
    public function __construct($monitor)
    {
        $this->monitor = $monitor;
    }
    public function run()
    {
        curl_setopt($this->ch, CURLOPT_URL, $this->monitor->getIp());
        $request = json_decode(curl_exec($this->ch), true);
        //some more db transactions
        $this->em->persist
        (
            (new Attempt())
                ->setMonitor($this->monitor)
                ->setTimestamp(new \DateTime())
                ->setLatency($request['latency'])
        );
        $this->em->flush();
    }
}

class ProbeWorker extends \Worker
{
    public function __construct($em)
    {
        $this->em = $em;
        $this->ch = curl_init();
    }
    public function run()
    {
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
    }
}

class DogeCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('wow:doge')
            ->setDescription('such speed')
        ;
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getManager();
        $monitors = $em->getRepository('WowABundle:Monitor')->findAll();
        $worker = new ProbeWorker($em);
        $worker->start();
        foreach($monitors as $monitor)
        {
            $job = new ProbeJob($monitor);
            $worker->stack($job);
        }
    }
}

This gives me an

这给了我一个

  [PDOException]                                     
  You cannot serialize or unserialize PDO instances 

error. What does that even mean? I profiled the performance of the old implementation where it would process it serially, and I spent half of my time on flushing. Splitting it into smaller chunks didn't help. Each iteration of the loop takes 1 second, so by parallelizing it, assuming the hardware was capable, I'd be able to significantly reduce the run time and have it scale to more websites. Is there a way to do this? How do I fix this?

错误。那有什么意思?我描述了旧的实现的性能,它将连续处理它,我花了一半的时间用于刷新。将它拆分成更小的块并没有帮助。循环的每次迭代需要1秒,因此通过并行化,假设硬件能够,我将能够显着减少运行时间并使其扩展到更多网站。有没有办法做到这一点?我该如何解决?

I am sure that each job is truly independent from each other, and there wouldn't be any conflicting db transactions. Everything is an insert or read.

我确信每个工作都是真正独立的,并且不存在任何冲突的数据库事务。一切都是插入或读取。

1 个解决方案

#1


0  

The worker should initialize curl in the run method, not the constructor.

worker应该在run方法中初始化curl,而不是构造函数。

The $ch resource is then available to stackables via $this->worker->ch during their execution.

然后,$ ch资源在执行期间通过$ this-> worker-> ch可用于stackables。

The error you are experiencing is because you are writing $em to the object scope, it doesn't descend from pthreads and so is not thread safe, pthreads therefore attempts to serialize the object for safe storage, but the object is not serializable so you experience the error you have shown.

您遇到的错误是因为您正在将$ em写入对象作用域,它不会从pthreads下降,因此不是线程安全的,因此pthreads会尝试序列化对象以进行安全存储,但该对象不可序列化,因此您遇到你所显示的错误。

The workaround is going to be to avoid setting that member, initialize an instance of the object for each Worker, I would probably store in static scope to avoid serialization and the unecessary overhead of mutex when accessing these complex objects.

解决方法是避免设置该成员,初始化每个Worker的对象实例,我可能会存储在静态范围内以避免序列化以及访问这些复杂对象时互斥的不必要开销。

#1


0  

The worker should initialize curl in the run method, not the constructor.

worker应该在run方法中初始化curl,而不是构造函数。

The $ch resource is then available to stackables via $this->worker->ch during their execution.

然后,$ ch资源在执行期间通过$ this-> worker-> ch可用于stackables。

The error you are experiencing is because you are writing $em to the object scope, it doesn't descend from pthreads and so is not thread safe, pthreads therefore attempts to serialize the object for safe storage, but the object is not serializable so you experience the error you have shown.

您遇到的错误是因为您正在将$ em写入对象作用域,它不会从pthreads下降,因此不是线程安全的,因此pthreads会尝试序列化对象以进行安全存储,但该对象不可序列化,因此您遇到你所显示的错误。

The workaround is going to be to avoid setting that member, initialize an instance of the object for each Worker, I would probably store in static scope to avoid serialization and the unecessary overhead of mutex when accessing these complex objects.

解决方法是避免设置该成员,初始化每个Worker的对象实例,我可能会存储在静态范围内以避免序列化以及访问这些复杂对象时互斥的不必要开销。