
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/reuse_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_reuse_executor.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_reuse_executor.py:


Reuse Executor
==============

This example highlights the ``loky`` API to reuse a ``ReusableProcessPool``.

The factory ``get_reusable_executor`` provides an executor. As long as this
executor is not in a broken state, it is reused for all the computation.

.. GENERATED FROM PYTHON SOURCE LINES 10-53




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    All the jobs were run in a process different from main process
    All the computation where run in a single `ProcessPoolExecutor`






|

.. code-block:: Python


    import os
    from loky import get_reusable_executor


    def func_async(i):
        pid = os.getpid()
        return (2 * i, pid)


    def test_1():
        executor = get_reusable_executor(max_workers=1)
        return executor.submit(func_async, 1)


    def test_2():
        executor = get_reusable_executor(max_workers=1)
        return executor.submit(func_async, 2)


    def test_3():
        executor = get_reusable_executor(max_workers=1)
        return executor.submit(func_async, 3)


    f1 = test_1()
    f2 = test_2()
    f3 = test_3()

    main_pid = os.getpid()
    results = [f1.result(), f2.result(), f3.result()]

    pids = [pid for _, pid in results]

    for i, (val, pid) in enumerate(results):
        assert val == 2 * (i + 1)
        assert pid != main_pid
    print("All the jobs were run in a process different from main process")

    assert len(set(pids)) == 1
    print(
        "All the computation where run in a single `ProcessPoolExecutor`"
    )


.. _sphx_glr_download_auto_examples_reuse_executor.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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