Created
March 8, 2018 11:40
-
-
Save tmbdev/6b3e6d34a902ca0b95f3cd851a315bb2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from time import sleep\n", | |
"from random import random\n", | |
"import os, sys\n", | |
"from socket import gethostname\n", | |
"from dask.distributed import Queue\n", | |
"from distributed import Client\n", | |
"client = Client()\n", | |
"client" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def read_fun(queues, src):\n", | |
" count = 0\n", | |
" for record in read_records(src):\n", | |
" shard = count % len(queues)\n", | |
" queues[shard].put(record)\n", | |
" count += 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def write_fun(dest, queue):\n", | |
" count = 0\n", | |
" with RecordWriter(dest) as stream:\n", | |
" while True:\n", | |
" record = queue.get()\n", | |
" if record is None: break\n", | |
" stream.write(record)\n", | |
" count += 1\n", | |
" return count" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"shards = list_shards(...)\n", | |
"queues = [None] * len(shards)\n", | |
"results = [None] * len(shards)\n", | |
"for i, s in enumerate(shards):\n", | |
" queues[i] = Queue()\n", | |
" results[i] = client.submit(write_fun, s, queues[i])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"sources = list_sources(...)\n", | |
"inputs = [None] * len(sources)\n", | |
"for s in sources:\n", | |
" inputs[i] = client.submit(read_fun(queues, s))" | |
] | |
} | |
], | |
"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.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment