在symfony命令中启动线程时发生致命错误 [英] Fatal error while starting threads within symfony 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');
}
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屋!