2016-07-27 127 views
1

我尝试在Symfony2命令中启动一个简单线程(使用pthreads ext v3 for php 7创建)。但我不知道是否因为不可序列化的闭包而导致错误(我不在任何地方使用闭包)。在symfony命令中启动线程时发生致命错误

命令:

<?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 

哪里是失败的呢?

回答

2

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

这工作:

$job->start(PTHREADS_INHERIT_ALL^PTHREADS_INHERIT_CLASSES); 

如果这不工作(这不完全一样,调用start无额外的价值):

$job->start(PTHREADS_INHERIT_ALL); 
+0

好的,这将工作的空线程。但它会删除所有加载的用户定义的类:-(请参阅此[示例]中的注释(https://github.com/krakjoe/pthreads/blob/master/examples/SelectiveInheritance.php) –

+0

u-nik:有你解决了这个问题? –

相关问题