gpt4 book ai didi

php - “Serialization of '在Symfony应用程序中调用\Threaded类(pthread)时关闭' is not allowed”

转载 作者:行者123 更新时间:2023-12-03 13:08:03 26 4
gpt4 key购买 nike

我无法让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中的排除列表中排除 ThreadThreadedWorker命名空间,但未能解决。

有人可以向我解释我在做什么错。我目前不确定是否可以使用带有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";
}
}

环境详细信息:
  • PHP 7.2.9(CLI)(内置:2018年8月15日23:10:01)(ZTS MSVC15(Visual C++ 2017)x64)
  • Pthreads v3内置:2018/5/25上午8:44
  • Symfony版本4.1.4
  • 最佳答案

    在symfony(2.8)命令中,我都没有使thread(3)工作。
    我的PHP版本:
    PHP 7.2.9-dev(CLI)(内置:2018年9月5日14:13:58)(ZTS)

    但作为替代解决方案:

  • 创建一个具有线程逻辑
  • 的简单php文件
  • 使用popen
  • 运行该php文件

    关于php - “Serialization of '在Symfony应用程序中调用\Threaded类(pthread)时关闭' is not allowed”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52194166/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com