{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import visualization\n",
    "import os\n",
    "import progressbar\n",
    "from joblib import Parallel, delayed\n",
    "from PIL import Image\n",
    "import pickle\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def my_long_task(i):\n",
    "    if 'pbar' in globals() and 'counter' in globals():\n",
    "        global counter\n",
    "        counter += 1\n",
    "        pbar.update(counter)\n",
    "    time.sleep(1)\n",
    "    print('{} is done'.format(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "                                                                               \r",
      "\r",
      "N/A% 0 |                                                            |   0.0 s/B"
     ]
    }
   ],
   "source": [
    "workers = 10\n",
    "\n",
    "widgets = [progressbar.Percentage(), ' ', progressbar.Counter(), ' ', progressbar.Bar(), ' ',\n",
    "           progressbar.FileTransferSpeed()]\n",
    "\n",
    "elements = list(range(20))\n",
    "pbar = progressbar.ProgressBar(widgets=widgets, maxval=len(elements)).start()\n",
    "counter = 0\n",
    "\n",
    "Parallel(n_jobs=workers, backend='multiprocessing')(delayed(my_long_task)(i) for i in elements)\n",
    "# Parallel(n_jobs=workers, backend='threading')(delayed(my_long_task)(i) for i in elements)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
