70646

Boostraping symfony 2 for pthread from command

I'm trying to start threads from symfony 2 command.

protected function execute(InputInterface $input, OutputInterface $output) { $parser = $this->getContainer()->get('app.article.parser'); $providers = $parser->getProviders(); foreach ($providers as $name=>$provider) { $parseThread = new ParseThread($parser, $name); $parseThread->start(); } }

ParseThread class:

class ParseThread extends \Thread { private $parser; private $providerName; function __construct (Parser $parser, $providerName) { $this->parser = $parser; $this->providerName = $providerName; } public function run () { $this->parser->parse($this->providerName); } }

$this->parser->parse(...) should, as I understand it be started in new thread. Inside parse method, based on string $providerName, bunch of classes are initialized.

Problem here is that new thread does not have Symfony 2 bootstrapped and thus has no composer autoloading - so my classes are not being loaded.

Is there any sensible way to bootstrap Symfony 2 in each thread, thus allowing for autoloading and all other things to work normally ?

Answer1:

PThreads doesn't inherit autoloading. This, however, shouldn't be a big deal. You can just do:

<?php class ParseThread extends \Thread { private $parser; private $providerName; function __construct (Parser $parser, $providerName) { $this->parser = $parser; $this->providerName = $providerName; } public function run () { require_once 'location/to/vendor/autoload.php'; $this->parser->parse($this->providerName); } }

Answer2:

I ended up adding this to run method:

$loader = require_once $kernel_root_dir.'/bootstrap.php.cache'; require_once $kernel_root_dir.'/AppKernel.php'; $kernel = new \AppKernel($env, true);

Adding only autoloaded did in fact load all classes, but produced a lot of other errors. Above code will bootstrap entire symfony 2, which will probably produce a bit larger footprint in each thread.

Recommend

  • printing from ipad to receipt printer(Epson TM-T88v) thr wifi
  • .js help with the Date object and if/else
  • Issue with regex for website validation in JavaScript
  • Perl - Create a new array and pass it to a subroutine thread
  • wait() and notify() method , always IllegalMonitorStateException is happen and tell me current Threa
  • How to build several left join query in Entity Framework Core for several many to one related entiti
  • Displaying text without interrupts
  • Jtree not shown on adding a node!
  • Jtree not shown on adding a node!
  • Returning gracefully from Matlab back to python
  • Connection string between IIS and SQL server
  • Exception: System.ArgumentException: Keyword not supported: 'initial catalog' when hard co
  • Programmatically update service worker if update() is not available
  • What is wrong with this mysql connection string?
  • Global PHP class in functions?
  • Swiftmailer email message returns error when attaching file
  • form_validation class does not load (codeigniter)
  • Is it possible to run clang with llc flags
  • Doctrine2 bulk import try to work with another entity
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Adding elements to a huge XML file
  • How integrated is Collada to OpenGL ES
  • Jackson Parser: ignore deserializing for type mismatch
  • Google Custom Search with transparent background
  • how to do an event when i swipe from fragment to the other
  • Akka Routing: Reply's send to router ends up as dead letters
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?