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

标签 php multithreading pthreads threadpool symfony4

我无法让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/

    相关文章:

    php - 在 require_once 中使用常量

    php - Laravel 5 邮件队列不工作

    multithreading - Node.js 异步逻辑行为

    c++ - 使用方法指针启动线程

    linux - linux内核如何决定下一个线程id

    linux - prctl(PR_SET_NAME PR_SET_NAME) 和 pthread_setname_np() 有什么区别?

    php - 是否可以防止类似页面加载HTML文件时额外加载JS和CSS文件?

    php - 无法理解错误 :Access denied for user 'Pratik' @'localhost' (using password: NO) got in php

    c - 递归函数和pthreads

    linux - 在 centOS 上,大量 float 的字符串流序列化比序列化 block 的 4 个 pthread-ts 更快。 std::threads 在 Windows 上更快