English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

New in Symfony 4.4: Better Control of Console Progress Bars

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).

New in Symfony 4.4: Better Control of Console Progress Bars symfony.com/blog/new-in-symfony-4-4-better-control-of-console-progress-bars

Tweet this

Comments

Will this also improve performance? I believe that if you update on every +1, and you do that for a loop of 50k, performance is dropping during the loop.
I guess the issue comes from your application. Usually it is caused by the garbage collector.
This will indeed improve performance in some cases. Redrawing the progress bar thousands of times per second won't work because of the IO on STDOUT necessary for that, which will become the bottleneck of your command.

That's actually the reason why such control was added in the first place with `setRedrawFrequency`.
Login with SymfonyConnect to post a comment