Skip to content

Instantly share code, notes, and snippets.

@tmbdev
Created March 8, 2018 11:41
Show Gist options
  • Save tmbdev/4f041fa0825abfbb6cc09e80b51faf24 to your computer and use it in GitHub Desktop.
Save tmbdev/4f041fa0825abfbb6cc09e80b51faf24 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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