.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/parallel/parallel_with_tqdm.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_parallel_parallel_with_tqdm.py: ================================================= Using tqdm to track progress with joblib.Parallel ================================================= This example demonstrates how to use a ``tqdm`` progress bar together with :class:`joblib.Parallel`. We present three main approaches: 1. :ref:`solution_generator`, via ``return_as='generator'`` to update the progress bar as tasks finish. This works well for homogeneous tasks but has some limitations for heterogeneous ones. 2. :ref:`solution_unordered_generator`, which works for heterogeneous tasks but does not preserve the tasks' order. 3. :ref:`solution_subclass`, which requires more code but allows accurate progress reporting independently of the ``return_as`` option. Note that ``return_as='generator'`` is not available for the ``'multiprocessing'`` backend. .. GENERATED FROM PYTHON SOURCE LINES 24-31 Task definition =============== We first define a task that sleeps for a given amount of time and returns that value. We also define a list of sleep durations that will be used. Note that the task durations are heterogeneous, simulating a situation where not all tasks take the same amount of time to complete. .. GENERATED FROM PYTHON SOURCE LINES 31-43 .. code-block:: Python import time def task(t): time.sleep(0.1 * t) return t times = [7, 2, 3, 5, 6, 4, 1] .. GENERATED FROM PYTHON SOURCE LINES 44-59 A first naive solution ====================== .. warning:: This solution reports the number of *dispatched* tasks rather than the number of *completed* tasks. It is presented for pedagogical purposes and should not be used in practice. A ``tqdm`` progress bar takes an iterable as input and returns an iterable. A :class:`~joblib.Parallel` call also consumes an iterable. A straightforward solution is therefore to wrap the input iterable with ``tqdm`` before passing it to :class:`~joblib.Parallel`. The progress bar then reflects the number of tasks dispatched to the workers. .. GENERATED FROM PYTHON SOURCE LINES 59-68 .. code-block:: Python from tqdm import tqdm from joblib import Parallel, delayed p = Parallel(n_jobs=2, pre_dispatch=4, batch_size=1) out = p(delayed(task)(t) for t in tqdm(times)) print(*out) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/7 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: parallel_with_tqdm.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: parallel_with_tqdm.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_