.. 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 ` 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` with worker pid=185 | .. code-block:: default 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` with " f"worker pid={pids[0]}" ) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.174 seconds) .. _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-python :download:`Download Python source code: reuse_executor.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: reuse_executor.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_