1

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?

u-nik
  • 488
  • 4
  • 12

1 Answers1

2

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);
john_k
  • 21
  • 2
  • Okay, this will work for the empty thread. But it will drop all loaded user defined classes :-( See the note in this [example](https://github.com/krakjoe/pthreads/blob/master/examples/SelectiveInheritance.php) – u-nik Jul 27 '16 at 15:52