【问题标题】:"Serialization of 'Closure' is not allowed" when calling a \Threaded class (pthreads) within a Symfony app在 Symfony 应用程序中调用 \Threaded 类(pthreads)时,“不允许序列化 'Closure'”
【发布时间】:2019-02-11 03:10:54
【问题描述】:

我无法让 PthreadsV3 与 Symfony 4.1.4 一起使用。

在单个 PHP 文件中运行简单的概念证明可以按预期工作,但是当移动到 Symfony ContainerAwareCommand 时,我会遇到以下错误。

致命错误:未捕获的异常:“闭包”的序列化不是 [无活动文件]中允许:0 堆栈跟踪:

0 {main} 在第 0 行 [no active file] 中抛出

我感觉这与 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 和 php 版本是 PHP 7.2.9-dev (cli) (built: Sep 5 2018 14:13:58) ( ZTS ) 当任务添加使用提交方法进入池,它正在抛出该消息。

标签: php multithreading pthreads threadpool symfony4


【解决方案1】:

在 symfony(2.8) 命令中,我没有让 thread(3) 工作。 我的php版本: PHP 7.2.9-dev (cli) (build: Sep 5 2018 14:13:58) (ZTS)

但作为替代解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多