在symfony命令中启动线程时发生致命错误 [英] Fatal error while starting threads within symfony command

查看:64
本文介绍了在symfony命令中启动线程时发生致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Symfony2命令中启动一个简单线程(使用php 7的pthreads ext v3创建).但是我想知道是否由于非可序列化的关闭而导致错误(我在任何地方都没有使用关闭).

命令:

<?php
public function execute(InputInterface $input, OutputInterface $output)
{
    $job = new JobThread();

    $output->writeln('Starting thread...');
    $job->start();

    $output->writeln('Waiting for thread to finish executing...');
    $job->join();

    $output->writeln('Thread finished');

}

JobThread类

<?php
class JobThread extends Thread
{
    public function run()
    {
        echo 'Run' . PHP_EOL;
        sleep(3);
        echo 'End' . PHP_EOL;
    }
}

如果我执行命令,则会得到以下输出:

Starting thread...
PHP 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

如果我在命令上下文之外启动线程...

$job = new ThreadJob();

echo 'Starting thread...' . PHP_EOL;
$job->start();

echo 'Waiting for thread to finish executing...' . PHP_EOL;
$job->join();

echo 'Thread finished' . PHP_EOL;

我得到了预期的输出:

Starting thread...
Waiting for thread to finish executing...
Run
End
Thread finished

故障点在哪里?

解决方案

我不知道为什么会这样,但是以下内容可能是一些提示:

这有效:

$job->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);

这在行不通的地方(与调用start而没有额外的值完全一样):

$job->start(PTHREADS_INHERIT_ALL);

i try to start a simple thread (create with the pthreads ext v3 for php 7) within a Symfony2 command. But i wonder if i get an error because of non serializable closure (i don't use a closure anywhere).

The Command:

<?php
public function execute(InputInterface $input, OutputInterface $output)
{
    $job = new JobThread();

    $output->writeln('Starting thread...');
    $job->start();

    $output->writeln('Waiting for thread to finish executing...');
    $job->join();

    $output->writeln('Thread finished');

}

The JobThread class

<?php
class JobThread extends Thread
{
    public function run()
    {
        echo 'Run' . PHP_EOL;
        sleep(3);
        echo 'End' . PHP_EOL;
    }
}

If i execute the command, i get the following output:

Starting thread...
PHP 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

If i start the thread outside of the command context...

$job = new ThreadJob();

echo 'Starting thread...' . PHP_EOL;
$job->start();

echo 'Waiting for thread to finish executing...' . PHP_EOL;
$job->join();

echo 'Thread finished' . PHP_EOL;

i get the expected output:

Starting thread...
Waiting for thread to finish executing...
Run
End
Thread finished

Where is the point of failure?

解决方案

I do not know why this happens, but the following might be some hint:

This works:

$job->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);

Where this does not work (which does exactly the same as calling start without an extra value):

$job->start(PTHREADS_INHERIT_ALL);

这篇关于在symfony命令中启动线程时发生致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆