
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/reuseable_executor.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_reuseable_executor.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_reuseable_executor.py:


Advanced Executor Setup
=======================

It is possible to reuse an executor even if it has a complicated setup. When
using the parameter `reuse=True`, the executor is resized if needed but the
arguments stay the same.

.. GENERATED FROM PYTHON SOURCE LINES 9-52







.. code-block:: Python


    from time import sleep
    import multiprocessing as mp
    import loky
    from loky import get_reusable_executor

    # Store the initialization status in a global variable of a module.
    loky._INITIALIZER_STATUS = "uninitialized"


    def initializer(x):
        print(f"[{mp.current_process().name}] init")
        loky._INITIALIZER_STATUS = x


    def return_initializer_status(delay=0):
        sleep(delay)

        return getattr(loky, "_INITIALIZER_STATUS", "uninitialized")


    executor = get_reusable_executor(
        max_workers=2,
        initializer=initializer,
        initargs=("initialized",),
        context="loky",
        timeout=1000,
    )

    assert loky._INITIALIZER_STATUS == "uninitialized"
    executor.submit(return_initializer_status).result()
    assert executor.submit(return_initializer_status).result() == "initialized"

    # With reuse=True, the executor use the same initializer
    executor = get_reusable_executor(max_workers=4, reuse=True)
    for x in executor.map(return_initializer_status, [0.5] * 4):
        assert x == "initialized"

    # With reuse='auto', the initializer is not used anymore as a new executor
    # is created.
    executor = get_reusable_executor(max_workers=4)
    for x in executor.map(return_initializer_status, [0.1] * 4):
        assert x == "uninitialized"


.. _sphx_glr_download_auto_examples_reuseable_executor.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: reuseable_executor.ipynb <reuseable_executor.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: reuseable_executor.py <reuseable_executor.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: reuseable_executor.zip <reuseable_executor.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
