.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` 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. .. rst-class:: sphx-glr-script-out 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` with worker pid=463 | .. code-block:: python import os import argparse from loky import get_reusable_executor def func_async(i): import os 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 = [v[1] for v 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` with " "worker pid={}".format(pids[0])) **Total running time of the script:** ( 0 minutes 0.155 seconds) .. _sphx_glr_download_auto_examples_reuse_executor.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: reuse_executor.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: reuse_executor.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_