Symfony2 command within an asynchronous subprocess Symfony2 command within an asynchronous subprocess symfony symfony

Symfony2 command within an asynchronous subprocess


Well, I found out what happened...

I use multiple environments: DEV, TEST and PROD.

And I also use differents servers.

So the DEV environment is my own machine with a simple mongodb configuration.But the TEST environment is on other server with a replica set configuration...

Now the error get full sense: "[MongoWriteConcernException] 127.0.0.1:27017: not master"

To solve it, I've just added the environment parameter (--env=) to the process and everything worked like a charm:

$process = new Process("php ../app/console my:command $country --env=test");

Actually, to get the correct environment I use this:

$this->get('kernel')->getEnvironment();

Which let's my code as follows:

$process = new Process("php ../app/console my:command $country --env=".$this->get('kernel')->getEnvironment());

Maybe is not a beautifull way to do it, but it works for me :)


Disclamer: This might be a bit overkill for what you're trying to do :)

I would choose an opposite way to do it: pthreads

First, quick examination of StackOverflow showed me a really nice example of using pthreads: Multi-threading is possible in php

Then, knowing that you could invoke your command from another command:

http://www.craftitonline.com/2011/06/calling-commands-within-commands-in-symfony2/

... lets you piece all the parts. It's a bit complicated but it does the job.


In case you want to execute your code completely async in Symfony2/3 there is AsyncServiceCallBundle for that.

You should just call it like this:

$this->get('krlove.async')->call('your_service_id', 'method_name', [$arg1, $arg2]);

Internally it uses this approach to run your code in background.