.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\parallelization_threading\demoMultiProcessing.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_parallelization_threading_demoMultiProcessing.py: Multi processing =================== This is a modified example from the python documentation. The only difference is the ``set_executable`` section at the start. Please notice that you cannot use methods from the itom module in any worker thread. Alternative approaches for multiprocessing are python ``threading`` module and ``asyncio``. Or use ``subprocess``. .. GENERATED FROM PYTHON SOURCE LINES 10-16 .. code-block:: default from multiprocessing import Pool, TimeoutError import multiprocessing import time import os .. GENERATED FROM PYTHON SOURCE LINES 18-19 Demo function for parallelization. .. GENERATED FROM PYTHON SOURCE LINES 19-23 .. code-block:: default def func(x): print("return x*x with x = ", x) return x * x .. GENERATED FROM PYTHON SOURCE LINES 24-25 Python executable is required .. GENERATED FROM PYTHON SOURCE LINES 25-67 .. code-block:: default pythonPath = ui.getOpenFileName( "Set path of python.exe", "C:/itom/3rdParty/Python/python.exe", "Python Executable (*.exe)", ) if pythonPath and os.path.exists(pythonPath): # set the path of the python executable (embedded python is required) multiprocessing.set_executable(pythonPath) with Pool(processes=4) as pool: # print "[0, 1, 4,..., 81]" print(pool.map(func, range(10))) # print same numbers in arbitrary order for i in pool.imap_unordered(func, range(10)): print(i) # evaluate "f(20)" asynchronously res = pool.apply_async(func, (20,)) # runs in *only* one process print(res.get(timeout=1)) # prints "400" # evaluate "os.getpid()" asynchronously res = pool.apply_async(os.getpid, ()) # runs in *only* one process print(res.get(timeout=1)) # prints the PID of that process # launching multiple evaluations asynchronously *may* use more processes multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)] print([res.get(timeout=1) for res in multiple_results]) # make a single worker sleep for 10 secs res = pool.apply_async(time.sleep, (10,)) try: print(res.get(timeout=1)) except TimeoutError: print("We lacked patience and got a multiprocessing.TimeoutError") print("For the moment, the pool remains available for more work") # exiting the 'with'-block has stopped the pool print("Now the pool is closed and no longer available") .. _sphx_glr_download_11_demos_python_packages_parallelization_threading_demoMultiProcessing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demoMultiProcessing.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demoMultiProcessing.ipynb `