"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def inc(x: int) -> int:\n",
" return x + 1\n",
"\n",
"future = client.submit(inc, 10)\n",
"future"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:29.926427Z",
"iopub.status.busy": "2022-07-27T19:14:29.925594Z",
"iopub.status.idle": "2022-07-27T19:14:29.939065Z",
"shell.execute_reply": "2022-07-27T19:14:29.938178Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await future"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Collections\n",
"-----------\n",
"\n",
"Note that blocking operations like the `.compute()` method aren't ok to use in asynchronous mode. Instead you'll have to use the `Client.compute` method."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:29.942194Z",
"iopub.status.busy": "2022-07-27T19:14:29.941780Z",
"iopub.status.idle": "2022-07-27T19:14:30.298646Z",
"shell.execute_reply": "2022-07-27T19:14:30.297952Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"Dask DataFrame Structure:
\n",
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" name | \n",
" x | \n",
" y | \n",
"
\n",
" \n",
" npartitions=30 | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2000-01-01 | \n",
" int64 | \n",
" object | \n",
" float64 | \n",
" float64 | \n",
"
\n",
" \n",
" 2000-01-02 | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2000-01-30 | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2000-01-31 | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
"
\n",
"
\n",
"Dask Name: make-timeseries, 30 tasks
"
],
"text/plain": [
"Dask DataFrame Structure:\n",
" id name x y\n",
"npartitions=30 \n",
"2000-01-01 int64 object float64 float64\n",
"2000-01-02 ... ... ... ...\n",
"... ... ... ... ...\n",
"2000-01-30 ... ... ... ...\n",
"2000-01-31 ... ... ... ...\n",
"Dask Name: make-timeseries, 30 tasks"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import dask\n",
"df = dask.datasets.timeseries()\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:30.302926Z",
"iopub.status.busy": "2022-07-27T19:14:30.302514Z",
"iopub.status.idle": "2022-07-27T19:14:30.329883Z",
"shell.execute_reply": "2022-07-27T19:14:30.329331Z"
}
},
"outputs": [],
"source": [
"df = df.persist() # persist is non-blocking, so it's ok"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:30.333306Z",
"iopub.status.busy": "2022-07-27T19:14:30.332799Z",
"iopub.status.idle": "2022-07-27T19:14:30.341749Z",
"shell.execute_reply": "2022-07-27T19:14:30.341195Z"
}
},
"outputs": [],
"source": [
"total = df[['x', 'y']].sum() # lazy computations are also ok"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:30.347194Z",
"iopub.status.busy": "2022-07-27T19:14:30.346708Z",
"iopub.status.idle": "2022-07-27T19:14:30.350518Z",
"shell.execute_reply": "2022-07-27T19:14:30.349930Z"
}
},
"outputs": [],
"source": [
"# total.compute() # but compute is bad, because compute blocks until done"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:30.363398Z",
"iopub.status.busy": "2022-07-27T19:14:30.363198Z",
"iopub.status.idle": "2022-07-27T19:14:30.401604Z",
"shell.execute_reply": "2022-07-27T19:14:30.400873Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"Future: finalize\n",
" status: \n",
"\n",
"\n",
"pending,\n",
"\n",
"\n",
"\n",
" type: NoneType,\n",
"\n",
"\n",
" key: finalize-c005c96bb73e558555c311bfabfc6ba2"
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"future = client.compute(total)\n",
"future"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2022-07-27T19:14:30.405415Z",
"iopub.status.busy": "2022-07-27T19:14:30.405220Z",
"iopub.status.idle": "2022-07-27T19:14:31.247222Z",
"shell.execute_reply": "2022-07-27T19:14:31.246606Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"x 239.172407\n",
"y 762.941155\n",
"dtype: float64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await future"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Within a script\n",
"---------------\n",
"\n",
"Running async/await code in Jupyter is a bit atypical. Jupyter already has an event loop running, so it's easy to use async/await syntax directly within it. In a normal Python script this won't be the case. Here is an example script that should run within a normal Python interpreter or as a script.\n",
"\n",
"```python\n",
"import asyncio\n",
"from dask.distributed import Client\n",
"\n",
"\n",
"def inc(x: int) -> int:\n",
" return x + 1\n",
"\n",
"\n",
"async def f():\n",
" async with Client(asynchronous=True) as client:\n",
" future = client.submit(inc, 10)\n",
" result = await future\n",
" print(result)\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" asyncio.get_event_loop().run_until_complete(f())\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}