I'm using Symfony 3 and php pthreads in my project. I need to access the entity manager from a Thread. I read some way how to do this on StackOverflow and the issue tracker of pthreads.
class BaseBackgroundThread extends \Thread
{
private $container;
private $env;
private $debug;
public function __construct($env, $debug)
{
$this->env = $env;
$this->debug = $debug;
}
public function run()
{
require 'app/autoload.php';
include_once 'var/bootstrap.php.cache';
$kernel = new \AppKernel($this->env, $this->debug);
$kernel->loadClassCache();
$kernel->boot();
$this->container = $kernel->getContainer();
}
public function start(int $options = null)
{
return parent::start(PTHREADS_INHERIT_NONE);
}
}
But there is one problem. I get a segmentation fault (core dumped)
error when i'm calling $kernel->getContainer()
.
Update:
I realized that $kernel->getContainer()
doesn't cause crash. But $this->container = $kernel->getContainer()
causes crash.
Update 2:
$this->container = $kernel->getContainer()
causes crash
self::$container = $kernel->getContainer()
causes crash too
$container = $kernel->getContainer()
doesn't cause crash.
Update 3:
I tried to run it on php version PHP 5.6.27RC1 (cli) (built: Oct 12 2016 02:24:27)
And got this error.
Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in /root/TheBuggestBot/src/TheBuggestBot/CoreBundle/BackgroundThread/BaseBackgroundThread.php:27
zend_mm_heap corrupted
But in this (https://github.com/krakjoe/pthreads/issues/369) discussion said that this code works.
P.S. Issue on github -> https://github.com/krakjoe/pthreads/issues/629