mirror of
https://github.com/gmihaila/ml_things.git
synced 2021-10-04 01:29:04 +03:00
Created using Colaboratory
This commit is contained in:
131
parallel_processing.ipynb
Normal file
131
parallel_processing.ipynb
Normal file
@@ -0,0 +1,131 @@
|
||||
{
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "parallel_processing.ipynb",
|
||||
"version": "0.3.2",
|
||||
"provenance": [],
|
||||
"collapsed_sections": [],
|
||||
"include_colab_link": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"name": "python3",
|
||||
"display_name": "Python 3"
|
||||
}
|
||||
},
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "view-in-github",
|
||||
"colab_type": "text"
|
||||
},
|
||||
"source": [
|
||||
"<a href=\"https://colab.research.google.com/github/gmihaila/machine_learning_toolbox/blob/master/parallel_processing.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ZupKtyWvGJp6",
|
||||
"colab_type": "text"
|
||||
},
|
||||
"source": [
|
||||
"## Multi processing Python\n",
|
||||
"\n",
|
||||
"https://stackoverflow.com/questions/10415028/how-can-i-recover-the-return-value-of-a-function-passed-to-multiprocessing-proce\n",
|
||||
"\n",
|
||||
"https://medium.com/practo-engineering/threading-vs-multiprocessing-in-python-7b57f224eadb\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"metadata": {
|
||||
"id": "L4tQR9DCGFfH",
|
||||
"colab_type": "code",
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/",
|
||||
"height": 68
|
||||
},
|
||||
"outputId": "5c08f732-24e1-48b6-e98f-8a0db1ba2ee8"
|
||||
},
|
||||
"source": [
|
||||
"from multiprocessing.dummy import Pool as ThreadPool\n",
|
||||
"import random\n",
|
||||
"from threading import Thread\n",
|
||||
"from multiprocessing import Process\n",
|
||||
"import multiprocessing\n",
|
||||
"import time\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"cpus = multiprocessing.cpu_count()\n",
|
||||
"size = 10000000 # Number of random numbers to add to list\n",
|
||||
"threads = cpus # Number of threads to create\n",
|
||||
"my_list = []\n",
|
||||
"\n",
|
||||
"for i in range(0, threads):\n",
|
||||
" my_list.append([])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def func(count, mylist):\n",
|
||||
" for i in range(count):\n",
|
||||
" mylist.append(random.random())\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def multithreaded():\n",
|
||||
" jobs = []\n",
|
||||
" for i in range(0, threads):\n",
|
||||
" thread = Thread(target=func, args=(size, my_list[i]))\n",
|
||||
" jobs.append(thread)\n",
|
||||
" # Start the threads\n",
|
||||
" for j in jobs:\n",
|
||||
" j.start()\n",
|
||||
" # Ensure all of the threads have finished\n",
|
||||
" for j in jobs:\n",
|
||||
" j.join()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def simple():\n",
|
||||
" for i in range(0, threads):\n",
|
||||
" func(size, my_list[i])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def multiprocessed():\n",
|
||||
" processes = []\n",
|
||||
" for i in range(0, threads):\n",
|
||||
" p = Process(target=func, args=(size, my_list[i]))\n",
|
||||
" processes.append(p)\n",
|
||||
" # Start the processes\n",
|
||||
" for p in processes:\n",
|
||||
" p.start()\n",
|
||||
" # Ensure all processes have finished execution\n",
|
||||
" for p in processes:\n",
|
||||
" p.join()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == \"__main__\":\n",
|
||||
" print(\"Cores: \", cpus)\n",
|
||||
" s = time.time()\n",
|
||||
" # multithreaded()\n",
|
||||
"# simple()\n",
|
||||
" multiprocessed()\n",
|
||||
" print(\"Seconds: \",(time.time() - s))\n",
|
||||
" print(\"Done!\")"
|
||||
],
|
||||
"execution_count": 9,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Cores: 2\n",
|
||||
"Seconds: 3.593935966491699\n",
|
||||
"Done!\n"
|
||||
],
|
||||
"name": "stdout"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user