{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from threading import Thread\n", "from multiprocessing import Process, Queue\n", "import time " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def do_work(start, end, result):\n", " sum = 0\n", " for i in range(start,end):\n", " sum += i\n", " result.append(sum)\n", " return" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if __name__=='__main__':\n", " s_time = time.time()\n", " START, END = 0, 80000000\n", " result = list()\n", " th1 = Thread(target=do_work, args=(START, END, result))\n", " th1.start()\n", " th1.join()\n", "print ('Result : ',sum(result),'time =',time.time()-s_time)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def multi_thread(thread_n=1):\n", " start_time = time.time()\n", " START, END = 0, 80000000\n", " result = list()\n", " \n", " end_time = end.time()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import multiprocessing.dummy as mp \n", "\n", "def do_print(s):\n", " print s\n", "\n", "if __name__==\"__main__\":\n", " p=mp.Pool(4)\n", " p.map(do_print,range(0,10)) # range(0,1000) if you want to replicate your example\n", " p.close()\n", " p.join()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Result : 3199999960000000 time = 4.5752880573272705\n" ] } ], "source": [ "s_time = time.time()\n", "START, END = 0, 80000000\n", "result = list()\n", "th1 = Thread(target=do_work, args=(START, END, result))\n", "th1.start()\n", "th1.join()\n", "print ('Result : ',sum(result),'time =',time.time()-s_time)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Result : 3199999960000000 time = 5.104726076126099\n" ] } ], "source": [ "s_time = time.time()\n", "START, END = 0, 80000000\n", "result = list()\n", "th1 = Thread(target=do_work, args=(START, 20000000, result))\n", "th2 = Thread(target=do_work, args=(20000000, 40000000, result))\n", "th3 = Thread(target=do_work, args=(40000000, 60000000, result))\n", "th4 = Thread(target=do_work, args=(60000000, 80000000, result))\n", "th1.start()\n", "th2.start()\n", "th3.start()\n", "th4.start()\n", "th1.join()\n", "th2.join()\n", "th3.join()\n", "th4.join()\n", "print('Result : ',sum(result),'time =',time.time()-s_time)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'int' object is not callable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;31m# if tmp == 'STOP' : break\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;31m# else: sum += tmp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 23\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Result : '\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'time ='\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0ms_time\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'int' object is not callable" ] } ], "source": [ "s_time = time.time()\n", "START, END = 0, 80000000\n", "# result = Queue()\n", "result = list()\n", "pr1 = Process(target=do_work, args=(START, 20000000, result))\n", "pr2 = Process(target=do_work, args=(20000000, 40000000, result))\n", "pr3 = Process(target=do_work, args=(40000000, 60000000, result))\n", "pr4 = Process(target=do_work, args=(60000000, 80000000, result))\n", "pr1.start()\n", "pr2.start()\n", "pr3.start()\n", "pr4.start()\n", "pr1.join()\n", "pr2.join()\n", "pr3.join()\n", "pr4.join()\n", "# result.put('STOP')\n", "# sum = 0\n", "# while True:\n", "# tmp = result.get()\n", "# if tmp == 'STOP' : break\n", "# else: sum += tmp\n", "print('Result : ',sum(result),'time =',time.time()-s_time)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "main line\n", "parent process: 11614\n", "process id: 11622\n", "function f\n", "parent process: 11622\n", "process id: 13423\n", "hello bob\n" ] } ], "source": [ "from multiprocessing import Process\n", "import os\n", "\n", "def info(title):\n", " print(title)\n", " print('parent process:', os.getppid())\n", " print('process id:', os.getpid())\n", "\n", "def f(name):\n", " info('function f')\n", " print('hello', name)\n", "\n", "# if __name__ == '__main__':\n", "info('main line')\n", "p = Process(target=f, args=('bob',))\n", "p.start()\n", "p.join()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.5" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }