我无法让PthreadsV3与Symfony 4.1.4一起使用。
在单个PHP文件中运行简单的概念证明可以按预期工作,但是当移至Symfony ContainerAwareCommand
时,会遇到以下错误。
Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in [no active file]:0 Stack trace:
0 {main} thrown in [no active file] on line 0
我感觉这与Symfony容器在\Threaded类中被复制和序列化有关。在bug report中提出建议后,我尝试在services.yaml中的排除列表中排除
Thread
,Threaded
和Worker
命名空间,但未能解决。有人可以向我解释我在做什么错。我目前不确定是否可以使用带有Pthreads的Symfony 4。
以下代码可从单个php文件运行。
<?php
$pool = new \Pool(4, TickerWorker::class, []);
for ($i = 0; $i < 10; $i++) {
$pool->submit(new TickerTask($i));
}
while ($pool->collect()) ;
$pool->shutdown();
echo "Pool done\n";
class TickerWorker extends \Worker
{
public function __construct()
{
}
}
class TickerTask extends \Threaded
{
protected $complete;
private $i;
public function __construct($i)
{
echo "$i start\n";
$this->i = $i;
$this->complete = false;
}
public function run()
{
sleep(rand(1, 3));
echo "$this->i done\n";
$this->complete = true;
}
public function isComplete()
{
return $this->complete;
}
}
这不起作用,并引发异常
<?php
namespace App\Command;
use App\Pthread\TickerTask;
use App\Pthread\TickerWorker;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Where App\Pthread\TickerWorker and App\Pthread\TickerTask
* are the classes as defined in the working example
*/
class CoconutTestCommand extends ContainerAwareCommand
{
protected static $defaultName = 'coconut:test';
protected function execute(InputInterface $input, OutputInterface $output)
{
$pool = new \Pool(4, TickerWorker::class, []);
for ($i = 0; $i < 10; $i++) {
$pool->submit(new TickerTask($i));
}
while ($pool->collect()) ;
$pool->shutdown();
echo "Pool done\n";
}
}
环境详细信息:
最佳答案
在symfony(2.8)命令中,我都没有使thread(3)工作。
我的PHP版本:
PHP 7.2.9-dev(CLI)(内置:2018年9月5日14:13:58)(ZTS)
但作为替代解决方案:
关于php - “Serialization of '在Symfony应用程序中调用\Threaded类(pthread)时关闭' is not allowed”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52194166/