{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- https://www.youtube.com/watch?v=xJCPpDlk9_w\n",
    "- [Functional Programming in Python: Parallel Processing with \"multiprocessing\"](https://www.youtube.com/watch?v=aysceqdGFw8)\n",
    "- http://uwpce-pythoncert.github.io/SystemDevelopment/threading-multiprocessing.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from collections import namedtuple\n",
    "import os\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Scientist = namedtuple('Scientist', ['name', 'field', 'born', 'nobel'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ada = Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False)\n",
    "ada"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),\n",
       " Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),\n",
       " Scientist(name='Marie Curie', field='physics', born=1867, nobel=True),\n",
       " Scientist(name='Tu Youyou', field='chemistry', born=1930, nobel=True),\n",
       " Scientist(name='Ada Yonath', field='chemistry', born=1939, nobel=True),\n",
       " Scientist(name='Vera Rubun', field='stronomy', born=1928, nobel=False),\n",
       " Scientist(name='Sally Ride', field='physics', born=1951, nobel=False))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scientists = (\n",
    "    Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),\n",
    "    Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),\n",
    "    Scientist(name='Marie Curie', field='physics', born=1867, nobel=True),\n",
    "    Scientist(name='Tu Youyou', field='chemistry', born=1930, nobel=True),\n",
    "    Scientist(name='Ada Yonath', field='chemistry', born=1939, nobel=True),\n",
    "    Scientist(name='Vera Rubun', field='stronomy', born=1928, nobel=False),\n",
    "    Scientist(name='Sally Ride', field='physics', born=1951, nobel=False)\n",
    ")\n",
    "scientists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def transform(x):\n",
    "    print(f'Process {os.getpid()} record {x.name}...')\n",
    "    time.sleep(1)\n",
    "    print(f'-> Process {os.getpid()} done processing record {x.name}')\n",
    "    return {'name': x.name, 'age': 2017 - x.born}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Process 1873 record Ada Lovelace...\n",
      "-> Process 1873 done processing record Ada Lovelace\n",
      "Process 1873 record Emmy Noether...\n",
      "-> Process 1873 done processing record Emmy Noether\n",
      "Process 1873 record Marie Curie...\n",
      "-> Process 1873 done processing record Marie Curie\n",
      "Process 1873 record Tu Youyou...\n",
      "-> Process 1873 done processing record Tu Youyou\n",
      "Process 1873 record Ada Yonath...\n",
      "-> Process 1873 done processing record Ada Yonath\n",
      "Process 1873 record Vera Rubun...\n",
      "-> Process 1873 done processing record Vera Rubun\n",
      "Process 1873 record Sally Ride...\n",
      "-> Process 1873 done processing record Sally Ride\n",
      "(Elapsed: 7.02s)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "({'age': 202, 'name': 'Ada Lovelace'},\n",
       " {'age': 135, 'name': 'Emmy Noether'},\n",
       " {'age': 150, 'name': 'Marie Curie'},\n",
       " {'age': 87, 'name': 'Tu Youyou'},\n",
       " {'age': 78, 'name': 'Ada Yonath'},\n",
       " {'age': 89, 'name': 'Vera Rubun'},\n",
       " {'age': 66, 'name': 'Sally Ride'})"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tic = time.time()\n",
    "result = tuple(map(transform, scientists))\n",
    "toc = time.time()\n",
    "print(f'(Elapsed: {toc-tic:.2f}s)')\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import multiprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Process 2010 record Ada Lovelace...\n",
      "Process 2011 record Emmy Noether...\n",
      "-> Process 2010 done processing record Ada Lovelace\n",
      "Process 2010 record Marie Curie...\n",
      "-> Process 2011 done processing record Emmy Noether\n",
      "Process 2011 record Tu Youyou...\n",
      "-> Process 2010 done processing record Marie Curie\n",
      "-> Process 2011 done processing record Tu Youyou\n",
      "Process 2010 record Ada Yonath...\n",
      "Process 2011 record Vera Rubun...\n",
      "-> Process 2010 done processing record Ada Yonath\n",
      "-> Process 2011 done processing record Vera Rubun\n",
      "Process 2010 record Sally Ride...\n",
      "-> Process 2010 done processing record Sally Ride\n",
      "(Elapsed: 4.06s)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'age': 202, 'name': 'Ada Lovelace'},\n",
       " {'age': 135, 'name': 'Emmy Noether'},\n",
       " {'age': 150, 'name': 'Marie Curie'},\n",
       " {'age': 87, 'name': 'Tu Youyou'},\n",
       " {'age': 78, 'name': 'Ada Yonath'},\n",
       " {'age': 89, 'name': 'Vera Rubun'},\n",
       " {'age': 66, 'name': 'Sally Ride'}]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tic = time.time()\n",
    "\n",
    "pool = multiprocessing.Pool(processes=2)\n",
    "result = pool.map(transform, scientists) \n",
    "\n",
    "toc = time.time()\n",
    "print(f'(Elapsed: {toc-tic:.2f}s)')\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Process 2076 record Ada Lovelace...\n",
      "Process 2077 record Emmy Noether...\n",
      "-> Process 2076 done processing record Ada Lovelace\n",
      "Process 2076 record Marie Curie...\n",
      "-> Process 2077 done processing record Emmy Noether\n",
      "Process 2077 record Tu Youyou...\n",
      "-> Process 2076 done processing record Marie Curie\n",
      "Process 2076 record Ada Yonath...\n",
      "-> Process 2077 done processing record Tu Youyou\n",
      "Process 2077 record Vera Rubun...\n",
      "-> Process 2076 done processing record Ada Yonath\n",
      "Process 2076 record Sally Ride...\n",
      "-> Process 2077 done processing record Vera Rubun\n",
      "-> Process 2076 done processing record Sally Ride\n",
      "(Elapsed: 4.07s)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'age': 202, 'name': 'Ada Lovelace'},\n",
       " {'age': 135, 'name': 'Emmy Noether'},\n",
       " {'age': 150, 'name': 'Marie Curie'},\n",
       " {'age': 87, 'name': 'Tu Youyou'},\n",
       " {'age': 78, 'name': 'Ada Yonath'},\n",
       " {'age': 89, 'name': 'Vera Rubun'},\n",
       " {'age': 66, 'name': 'Sally Ride'}]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tic = time.time()\n",
    "\n",
    "pool = multiprocessing.Pool(processes=2, maxtasksperchild=4)\n",
    "result = pool.map(transform, scientists) \n",
    "\n",
    "toc = time.time()\n",
    "print(f'(Elapsed: {toc-tic:.2f}s)')\n",
    "result"
   ]
  },
  {
   "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
