Skip to content

Instantly share code, notes, and snippets.

@stsievert
Created April 15, 2019 13:34
Show Gist options
  • Save stsievert/0e080e8b49bd15382972172e97252315 to your computer and use it in GitHub Desktop.
Save stsievert/0e080e8b49bd15382972172e97252315 to your computer and use it in GitHub Desktop.
Tornado yielding list
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/scott/anaconda3/lib/python3.6/site-packages/distributed/bokeh/core.py:57: UserWarning: \n",
"Port 8787 is already in use. \n",
"Perhaps you already have a cluster running?\n",
"Hosting the diagnostics dashboard on a random port instead.\n",
" warnings.warn('\\n' + msg)\n"
]
}
],
"source": [
"from tornado import gen\n",
"from dask.distributed import Future, default_client, futures_of, wait\n",
"import time\n",
"\n",
"from distributed import Client\n",
"client = Client()\n",
"\n",
"class SHA:\n",
" def __init__(self, x=1, use_time=False):\n",
" self.x = x\n",
" self.use_time = use_time\n",
" \n",
" @gen.coroutine\n",
" def _fit(self):\n",
" if self.use_time:\n",
" time.sleep(self.x)\n",
" else:\n",
" yield gen.sleep(self.x)\n",
" raise gen.Return(self)\n",
" \n",
" def fit(self):\n",
" return default_client().sync(self._fit)\n",
"\n",
"class Hyperband:\n",
" def __init__(self, use_time=False):\n",
" self.use_time = use_time\n",
" \n",
" @gen.coroutine\n",
" def _fit(self):\n",
" SHAs = [SHA(x=x, use_time=self.use_time) for x in [1, 2]]\n",
" SHAs = yield [SHA.fit() for SHA in SHAs]\n",
" raise gen.Return(self)\n",
" \n",
" def fit(self):\n",
" return default_client().sync(self._fit)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 453 ms, sys: 91.4 ms, total: 544 ms\n",
"Wall time: 2.01 s\n"
]
},
{
"data": {
"text/plain": [
"<__main__.Hyperband at 0x116a452e8>"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"Hyperband().fit()"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 591 ms, sys: 115 ms, total: 706 ms\n",
"Wall time: 3.05 s\n"
]
},
{
"data": {
"text/plain": [
"<__main__.Hyperband at 0x116ad1080>"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"Hyperband(use_time=True).fit()"
]
},
{
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment