PTHREADS SYMFONY 在 class extending \Thread 的 运行() 函数中传递 classes 和常量

PTHREADS SYMFONY pass classes and constant in run() function of the class extending \Thread

我一直在阅读有关使用 pthreads 和 symfony 的文章。

我的问题有点类似于那个问题中暴露的问题:Multi-threading in Symfony2

简而言之:我的后端在处理它应该处理的所有数据之前达到超时,然后它无法向我的前端发送回答案。

因此,尝试使用不同的线程似乎是绕过该问题的(一种或多种)解决方案(直到达到一定的限制,我知道这一点)。

通过阅读,我了解了 pthreads 工作原理的基础知识,并发现这篇文章与它非常相关:https://blog.madewithlove.be/post/thread-carefully/

我用一个基本的 symfony 项目做了一个示例案例来理解它:

[my_symf_project]下的主控制器class\src\AppBundle\Controller:

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        $tt = new TestThread('BOOOMSTICK');
        $tt->start();

        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
        ]);
    }
}

class 扩展线程位于:[my_symf_project]\src\AppBundle\DependencyInjection:

<?php
namespace AppBundle\DependencyInjection;

    class TestThread extends \Thread {
        public function __construct($text){
            $this->text = $text;
        }

        public function run(){
            $vendorAutoload= __DIR__.'/../../../vendor/autoload.php';
            require_once  $vendorAutoload;
            require_once  __DIR__.'/ClassOutsideThread.php';

            $cot = new ClassOutsideThread(' RUN ' . ' ' . $this->text);
            $cot->show();
        }

    }
    ?>

还有另一个 class(在 class 扩展线程之外)以及 [my_symf_project]\src\AppBundle\DependencyInjection:

<?php
namespace AppBundle\DependencyInjection;

class ClassOutsideThread {
    public function __construct($text){
        $this->text = $text;
    }

    public function show(){
        echo $this->text;
    }
}
?>

通过上面的代码,我设法在标准 symfony 空项目页面的顶部显示 'RUN BOOOMSTICK'。我知道这是可能的,因为我在 运行() 函数的开头添加了语句 "require_once __DIR__.'/ClassOutsideThread.php';"。

现在我遇到了几个关于如何 传递给子线程的问题:类 和其他 symfony 上下文参数 ,通过 运行() 函数.这是我想要得到的东西的愿望清单:

  1. 类 文件夹 [my_symf_proj]\src\AppBundle 下的文件:有没有一种快速的方法来加载该包下的所有 classes,比如自动加载有关在我的 class TestThread 中使用的 'vendor' 文件夹(取自:Boostraping symfony 2 for pthread from command)?
  2. 我想在 运行() 函数中使用 symfony 记录器。我已经尝试过:$GLOBALS['kernel']->getContainer()->get('logger'),但是我在 [my_symf_proj] \var\logs\dev 中得到了很大的空白.log,所以我想知道如何通过子线程访问 $GLOBALS['kernel']->getContainer()?
  3. 我想在 运行() 函数中使用我的 Doctrine 连接到 DB(对应于 symfony 参数集的那个)。我怎样才能做到这一点? (我也试过 $GLOBALS['kernel']->getContainer()->get('dbal.connection'),但似乎没有成功。)

PS:我知道在特定的子线程情况下使用 $GLOBALS['kernel']->getContainer() 也不是 symfony 的最佳实践,这似乎是一个方便的选择.

看看this post

1 您可以使用

$loader = require __DIR__.'/../../../../../../vendor/autoload.php';

2-3 如果您不必在线程之间共享对象,您可以在 运行 方法中轻松完成:

$kernel = new \AppKernel($this->env, $this->debug);
$kernel->loadClassCache();
$kernel->boot();
$this->container = $kernel->getContainer();
$this->container->get('doctrine_mongodb');
.....

通过提供的提示,我设法让它工作。这是 symfony 3.0.*

中的线程示例

将问题中的 class TestThread 替换为以下一个,您将看到 symfony 上下文到达子线程:

        class TestThread extends \Thread {
        public function __construct($text){


                $this->text = $text;
            $this->kernelName = $GLOBALS['kernel']->getName();
            $this->kernelEnv = $GLOBALS['kernel']->getEnvironment();
            $this->kernelDebug = $GLOBALS['kernel']->isDebug();

        }

        public function run(){
           require_once __DIR__.'/../../../app/autoload.php';
           require_once __DIR__.'/../../../app/AppKernel.php';



            $kernelInThread = new \AppKernel($this->kernelEnv, $this->kernelDebug);
            $kernelInThread->loadClassCache();
            $kernelInThread->boot();


            $container = $kernelInThread->getContainer();


            $log = $container->get('logger');

            $log->info('THIS IS WORKING NOW');

            $con = $container->get('doctrine.dbal.default_connection');
            $q = new PdoQuery($con);
                $c = $q->getConnection()->prepare('select field from table');

             $c->execute();
             $res = $c->fetchAll();
             var_dump($res); 



            echo 'the end </br>';
       }
    }