从命令为 pthread 提升 symfony 2 [英] Boostraping symfony 2 for pthread from command

查看:37
本文介绍了从命令为 pthread 提升 symfony 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 symfony 2 命令启动线程.

I'm trying to start threads from symfony 2 command.

protected function execute(InputInterface $input, OutputInterface $output)
{
    $parser = $this->getContainer()->get('app.article.parser');
    $providers = $parser->getProviders();
    foreach ($providers as $name=>$provider) 
    {
        $parseThread = new ParseThread($parser, $name);
        $parseThread->start();
    }
}

ParseThread 类:

ParseThread class:

class ParseThread extends \Thread 
{

private $parser;
private $providerName;

function __construct (Parser $parser, $providerName) 
{
    $this->parser       = $parser;
    $this->providerName = $providerName;
}

public function run () 
{
    $this->parser->parse($this->providerName);
}

}

$this->parser->parse(...) 应该,据我所知,它应该在新线程中启动.在 parse 方法中,基于字符串 $providerName,初始化了一堆类.

$this->parser->parse(...) should, as I understand it be started in new thread. Inside parse method, based on string $providerName, bunch of classes are initialized.

这里的问题是新线程没有引导 Symfony 2,因此没有自动加载作曲家 - 所以我的类没有被加载.

Problem here is that new thread does not have Symfony 2 bootstrapped and thus has no composer autoloading - so my classes are not being loaded.

是否有任何明智的方法可以在每个线程中引导 Symfony 2,从而允许自动加载和所有其他事情正常工作?

Is there any sensible way to bootstrap Symfony 2 in each thread, thus allowing for autoloading and all other things to work normally ?

推荐答案

我最终添加了这个 to run 方法:

I ended up adding this to run method:

    $loader = require_once $kernel_root_dir.'/bootstrap.php.cache';
    require_once $kernel_root_dir.'/AppKernel.php';
    $kernel = new \AppKernel($env, true);

仅添加 autoloaded 确实加载了所有类,但产生了许多其他错误.上面的代码将引导整个 symfony 2,这可能会在每个线程中产生更大的占用空间.

Adding only autoloaded did in fact load all classes, but produced a lot of other errors. Above code will bootstrap entire symfony 2, which will probably produce a bit larger footprint in each thread.

这篇关于从命令为 pthread 提升 symfony 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆