New in Symfony 6.4: Subprocess Handler
October 24, 2023 • Published by Javier Eguiluz
Symfony 6.4 is backed by:
Consider the following Symfony console command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use Symfony\Component\Process\Process;
// ...
class MyCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$subProcess = new Process(['bin/console', 'cache:pool:prune']);
// ...
}
}
If you run this command as follows:
1
$ php -d memory_limit=-1 bin/console app:my-command
Which will be the memory limit of the cache:pool:prune
command run from inside
app:my-command
? It won't be -1
. The reason is that subprocesses in PHP
don't inherit the configuration of their parent processes. Instead, they use the
default configuration defined in the corresponding php.ini
files.
In Symfony 6.4 we're introducing a new PhpSubprocess class to solve this problem.
Whenever you need to run a subprocess with the same PHP configuration as the parent
process, use PhpSubprocess
instead of Process
to run that subprocess:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use Symfony\Component\Process\PhpSubprocess;
// ...
class MyCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
// this subprocess will inherit all the config of the parent process
$subProcess = new PhpSubprocess(['bin/console', 'cache:pool:prune']);
// ...
}
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.