[5bb4ae]: / Concurrent.ipynb

Download this file

91 lines (90 with data), 2.0 kB

{
 "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
}