--- a +++ b/Concurrent.ipynb @@ -0,0 +1,90 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Concurrent.Futures.ThreadPoolExecutor" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "from concurrent.futures import ThreadPoolExecutor #using parallel threads \n", + "import time #time module" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def task(n):\n", + " print(\"Processing {}\".format(n))\n", + " time.sleep(10) #time.sleep pause the program. So it can be used to chech working of multiple thread" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def main():\n", + " print(\"Starting ThreadPoolExecutor\")\n", + " with ThreadPoolExecutor(max_workers=3) as executor:\n", + " future = executor.submit(task, (2)) #working of multiple threads\n", + " future = executor.submit(task, (3))\n", + " future = executor.submit(task, (4))\n", + " print(\"All tasks complete\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Starting ThreadPoolExecutor\n", + "Processing 2\n", + "Processing 3\n", + "Processing 4\n", + "All tasks complete\n" + ] + } + ], + "source": [ + "if __name__ == '__main__':\n", + " main() #main function" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.15" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}