New in Symfony 4.4: Better Control of Console Progress Bars

Written by Symfony blog - - Aggregated on Friday November 8, 2019

Contributed by
Gabriel Ostrolucký
in #26339.

Progress bars are one of the most popular features of the Console component. They allow to show progress information while the command runs, which is ideal for long-running commands.

The most difficult thing when creating some progress bars is how to define their redraw frequency with the setRedrawFrequency() method. If you redraw it too often, the progress bar flickers too much but if you redraw it not too often, the progress bar looks unresponsive.

An additional problem is that sometimes you don't know how fast or slow will the command progress (e.g. when downloading some file, copying files, etc.).

In Symfony 4.4 we've improved the Symfony Console progress bars with two new methods which will help you control the redraw frequency:

1
2
3
4
5
6
7
8
use Symfony\Component\Console\Helper\ProgressBar;

$progressBar = new ProgressBar($output, 50000);
$progressBar->start();

$progressBar->setRedrawFrequency(100);
$progressBar->minSecondsBetweenRedraws(0.1);
$progressBar->maxSecondsBetweenRedraws(0.5);

The new minSecondsBetweenRedraws() and maxSecondsBetweenRedraws() methods will give you a greater control of the progress bar redraw frequency and will ensure that you don't redraw it either too much or too little.

Related to this, in Symfony 4.4 we've also set 0.1 as the value of the minSecondsBetweenRedraws property, so progress bars are not updated more than 10 times per second by default (see Pull Request #32424).

Be trained by Symfony experts - 2019-11-12 Clichy - 2019-11-12 Clichy - 2019-11-13 Cologne

« Security Corner: Twist and Shout - php[architect] - The site for PHP professionals

Laravel News - 7 Tips to Prevent, Detect, and Fix Bugs … »