Php PTHREADS SYMFONY传递类和类extensing\Thread的run()函数中的常量
我已经读了很多关于在symfony中使用pthreads的书 我的问题有点类似于在那个问题中暴露出来的: 简而言之:我的后端在处理它应该处理的所有数据之前达到了一个超时,然后它无法向我的前端发回应答 因此,尝试使用不同的线程似乎是绕过该问题的解决方案之一(直到某个极限,我知道) 通过阅读,我了解了pthreads如何工作的基本知识,并发现本文与之相关: 我用一个基本的symfony项目做了一个示例来理解它: [my_symf_project]\src\AppBundle\controller下的主控制器类:Php PTHREADS SYMFONY传递类和类extensing\Thread的run()函数中的常量,php,multithreading,pthreads,symfony,Php,Multithreading,Pthreads,Symfony,我已经读了很多关于在symfony中使用pthreads的书 我的问题有点类似于在那个问题中暴露出来的: 简而言之:我的后端在处理它应该处理的所有数据之前达到了一个超时,然后它无法向我的前端发回应答 因此,尝试使用不同的线程似乎是绕过该问题的解决方案之一(直到某个极限,我知道) 通过阅读,我了解了pthreads如何工作的基本知识,并发现本文与之相关: 我用一个基本的symfony项目做了一个示例来理解它: [my_symf_project]\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').'/..'),
]);
}
}
扩展线程的类位于:[my\u symf\u 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();
}
}
?>
<?php
namespace AppBundle\DependencyInjection;
class ClassOutsideThread {
public function __construct($text){
$this->text = $text;
}
public function show(){
echo $this->text;
}
}
?>
和另一个类(扩展线程的类之外)以及[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();
}
}
?>
<?php
namespace AppBundle\DependencyInjection;
class ClassOutsideThread {
public function __construct($text){
$this->text = $text;
}
public function show(){
echo $this->text;
}
}
?>
通过上面的代码,我设法在标准symfony空项目页面的顶部显示“RunBoomStick”。我理解这是可能的,因为我在run()函数的开头添加了语句“require_once uuu DIR_uu.'/ClassOutsideThread.php';”
现在我遇到了几个关于如何通过run()函数传递给childthreads的问题:类和其他symfony上下文参数。以下是我想要得到的东西的愿望清单:
$loader = require __DIR__.'/../../../../../../vendor/autoload.php';
2-3如果您不必在线程之间共享对象,您可以在run方法内轻松执行以下操作:
$kernel = new \AppKernel($this->env, $this->debug);
$kernel->loadClassCache();
$kernel->boot();
$this->container = $kernel->getContainer();
$this->container->get('doctrine_mongodb');
.....
看一看
1.你可以用
$loader = require __DIR__.'/../../../../../../vendor/autoload.php';
2-3如果您不必在线程之间共享对象,您可以在run方法内轻松执行以下操作:
$kernel = new \AppKernel($this->env, $this->debug);
$kernel->loadClassCache();
$kernel->boot();
$this->container = $kernel->getContainer();
$this->container->get('doctrine_mongodb');
.....
通过提供的提示,我设法让它工作起来。下面是symfony 3.0中的线程示例* 将问题中的TestThread类替换为以下类,您将看到symfony上下文到达childthread:
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>';
}
}
class TestThread扩展\Thread{
公共函数构造($text){
$this->text=$text;
$this->kernelName=$GLOBALS['kernel']->getName();
$this->kernelEnv=$GLOBALS['kernel']->getEnvironment();
$this->kernelDebug=$GLOBALS['kernel']->isDebug();
}
公共功能运行(){
需要_once uuu DIR uuu.'/../../../../app/autoload.php';
需要_once uuu DIR uuu.'/../../../../app/AppKernel.php';
$kernelInThread=new\AppKernel($this->kernelEnv,$this->kernelDebug);
$kernelInThread->loadClassCache();
$kernelInThread->boot();
$container=$kernelInThread->getContainer();
$log=$container->get('logger');
$log->info('现在正在工作');
$con=$container->get('doctrine.dbal.default_connection');
$q=新的PdoQuery($con);
$c=$q->getConnection()->prepare('select field from table');
$c->execute();
$res=$c->fetchAll();
var_dump($res);
回音“结尾”;
}
}
通过提供的提示,我设法让它工作起来。下面是symfony 3.0中的线程示例*
将问题中的TestThread类替换为以下类,您将看到symfony上下文到达childthread:
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>';
}
}
class TestThread扩展\Thread{
公共函数构造($text){
$this->text=$text;
$this->kernelName=$GLOBALS['kernel']->getName();
$this->kernelEnv=$GLOBALS['kernel']->getEnvironment();
$this->kernelDebug=$GLOBALS['kernel']->isDebug();
}
公共功能运行(){
需要_once uuu DIR uuu.'/../../../../app/autoload.php';
需要_once uuu DIR uuu.'/../../../../app/AppKernel.php';
$kernelInThread=new\AppKernel($this->kernelEnv,$this->kernelDebug);
$kernelInThread->loadClassCache();
$kernelInThread->boot();
$container=$kernelInThread->getContainer();
$log=$container->get('logger');
$log->info('现在正在工作');
$con=$container->get('doctrine.dbal.default_connection');
$q=新的PdoQuery($con);
$c=$q->getConnection()->prepare('select field from table');
$c->execute();
$res=$c->fetchAll();
var_dump($res);
回音“结尾”;
}
}
只要我在“require_one'/../../../../../var/bootstrap.php.cache';”之前加上“require_one'/../../../../../app/autoload.php”;“run()函数根本不起作用(没有显示回显消息)。我在Symfony 3.0.2上运行此测试,请问您使用哪个版本?无论如何,谢谢你的公关