.. 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_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. .. code-block:: python from time import sleep import multiprocessing as mp from loky import get_reusable_executor INITIALIZER_STATUS = "uninitialized" def initializer(x): global INITIALIZER_STATUS print("[{}] init".format(mp.current_process().name)) INITIALIZER_STATUS = x def return_initializer_status(delay=0): sleep(delay) global INITIALIZER_STATUS return INITIALIZER_STATUS executor = get_reusable_executor( max_workers=2, initializer=initializer, initargs=('initialized',), context="loky", timeout=1000) assert 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, [.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, [.1] * 4): assert x == 'uninitialized' **Total running time of the script:** ( 0 minutes 1.060 seconds) .. _sphx_glr_download_auto_examples_reuseable_executor.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: reuseable_executor.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: reuseable_executor.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_