How it works...

All the work is done by the withprogressbar function. It acts as a decorator, so we can apply it to any function with the @withprogressbar syntax.

That is very convenient because the code that reports progress is isolated from the code actually doing the work, which allows us to reuse it in many different cases.

To make a decorator that interacts with the decorated function while the function itself is running, we relied on Python generators:

gen = func(*args, **kwargs)
while True:
        progress = next(gen)
    except StopIteration as exc:
        return exc.value
        # display the progressbar

When we call the decorated function (in our example, the wait function), we will be in fact calling _func_with_progress ...

Get Modern Python Standard Library Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.