{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Multi processing\n\nThis is a modified example from the python documentation.\nThe only difference is the ``set_executable`` section at the start.\nPlease notice that you cannot use methods from the itom module\nin any worker thread.\nAlternative approaches for multiprocessing are \npython ``threading`` module and ``asyncio``. Or use ``subprocess``.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from multiprocessing import Pool, TimeoutError\nimport multiprocessing\nimport time\nimport os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demo function for parallelization. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def func(x):\n print(\"return x*x with x = \", x)\n return x * x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python executable is required \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pythonPath = ui.getOpenFileName(\n \"Set path of python.exe\",\n \"C:/itom/3rdParty/Python/python.exe\",\n \"Python Executable (*.exe)\",\n)\n\nif pythonPath and os.path.exists(pythonPath):\n # set the path of the python executable (embedded python is required)\n multiprocessing.set_executable(pythonPath)\n\n with Pool(processes=4) as pool:\n\n # print \"[0, 1, 4,..., 81]\"\n print(pool.map(func, range(10)))\n\n # print same numbers in arbitrary order\n for i in pool.imap_unordered(func, range(10)):\n print(i)\n\n # evaluate \"f(20)\" asynchronously\n res = pool.apply_async(func, (20,)) # runs in *only* one process\n print(res.get(timeout=1)) # prints \"400\"\n\n # evaluate \"os.getpid()\" asynchronously\n res = pool.apply_async(os.getpid, ()) # runs in *only* one process\n print(res.get(timeout=1)) # prints the PID of that process\n\n # launching multiple evaluations asynchronously *may* use more processes\n multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)]\n print([res.get(timeout=1) for res in multiple_results])\n\n # make a single worker sleep for 10 secs\n res = pool.apply_async(time.sleep, (10,))\n try:\n print(res.get(timeout=1))\n except TimeoutError:\n print(\"We lacked patience and got a multiprocessing.TimeoutError\")\n\n print(\"For the moment, the pool remains available for more work\")\n\n # exiting the 'with'-block has stopped the pool\n print(\"Now the pool is closed and no longer available\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }