-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #6: configures and writes HTML (part two)
- Loading branch information
Thomas Lehmann
committed
Jan 20, 2018
1 parent
4f677fe
commit 8ee6914
Showing
17 changed files
with
511 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import multiprocessing\n", | ||
"from contextlib import closing\n", | ||
"\n", | ||
"def parallel(worker, data):\n", | ||
" with closing(multiprocessing.Pool(multiprocessing.cpu_count())) as pool:\n", | ||
" for result in pool.map(worker, data):\n", | ||
" yield result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Sleep 9 seconds\n", | ||
"Sleep 1 seconds\n", | ||
"Sleep 10 seconds\n", | ||
"Sleep 7 seconds\n", | ||
"Sleep 9 seconds\n", | ||
"Sleep 3 seconds\n", | ||
"Sleep 3 seconds\n", | ||
"Sleep 10 seconds\n", | ||
"Sleep 2 seconds\n", | ||
"Sleep 8 seconds\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import time\n", | ||
"import random\n", | ||
"\n", | ||
"def simple_worker(data):\n", | ||
" wait_seconds = random.randint(1, 10)\n", | ||
" print(\"Sleep %d seconds\" % wait_seconds)\n", | ||
" time.sleep(wait_seconds)\n", | ||
" return data\n", | ||
" \n", | ||
"list(parallel(simple_worker, list(range(10))))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Sleep 4 seconds\n", | ||
"Sleep 7 seconds\n", | ||
"Sleep 8 seconds\n", | ||
"Sleep 8 seconds\n", | ||
"Sleep 5 seconds\n", | ||
"Sleep 2 seconds\n", | ||
"Sleep 10 seconds\n", | ||
"Sleep 4 seconds\n", | ||
"Sleep 9 seconds\n", | ||
"Sleep 9 seconds\n", | ||
"Consumer receives 9\n", | ||
"Consumer receives 1\n", | ||
"Consumer receives 5\n", | ||
"Consumer receives 7\n", | ||
"Consumer receives 0\n", | ||
"Consumer receives 2\n", | ||
"Consumer receives 3\n", | ||
"Consumer receives 8\n", | ||
"Consumer receives 6\n", | ||
"Consumer receives 4\n", | ||
"Waiting for consumer\n", | ||
"done\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"class Consumer(multiprocessing.Process):\n", | ||
" def __init__(self, result_queue):\n", | ||
" multiprocessing.Process.__init__(self)\n", | ||
" self.result_queue = result_queue\n", | ||
" self.keep_running = True\n", | ||
"\n", | ||
" def run(self):\n", | ||
" while self.keep_running:\n", | ||
" result = self.result_queue.get()\n", | ||
" print(\"Consumer receives %s\" % result)\n", | ||
" print(\"consumer stops now\")\n", | ||
"\n", | ||
"class Producer(multiprocessing.Process):\n", | ||
" def __init__(self, result_queue, value):\n", | ||
" multiprocessing.Process.__init__(self)\n", | ||
" self.result_queue = result_queue\n", | ||
" self.value = value\n", | ||
" self.start()\n", | ||
"\n", | ||
" def run(self):\n", | ||
" wait_seconds = random.randint(1, 10)\n", | ||
" print(\"Sleep %d seconds\" % wait_seconds)\n", | ||
" time.sleep(wait_seconds)\n", | ||
" self.result_queue.put(self.value)\n", | ||
"\n", | ||
"\n", | ||
"# Establish communication queues\n", | ||
"result_queue = multiprocessing.Queue()\n", | ||
"consumer = Consumer(result_queue)\n", | ||
"consumer.start()\n", | ||
"\n", | ||
"tasks = [Producer(result_queue, idx) for idx in range(10)]\n", | ||
"for task in tasks:\n", | ||
" task.join()\n", | ||
"\n", | ||
"print(\"Waiting for consumer\")\n", | ||
"consumer.keep_running = False\n", | ||
"result_queue.close()\n", | ||
"consumer.terminate()\n", | ||
"print(\"done\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.