{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Handle Evolving Workflows\n", "=========================\n", "\n", "For some workflows we don't know the extent of the computation at the outset. We need to do some computation in order to figure out the rest of the computation that we need to do. The computation grows and evolves as we do more work.\n", "\n", "As an example, consider a situation where you need to read many files and then based on the contents of those files, fire off additional work. You would like to read the files in parallel, and then within each file expose more parallelism.\n", "\n", "This example goes through three ways to handle this situation using [Dask Futures](https://docs.dask.org/en/latest/futures.html)\n", "\n", "1. Using `as_completed`\n", "2. Using `async/await`\n", "3. Launching tasks from tasks\n", "\n", "But first, lets run our code sequentially." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "0: Sequential code\n", "------------------" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:15:46.563685Z", "iopub.status.busy": "2022-07-27T19:15:46.563245Z", "iopub.status.idle": "2022-07-27T19:15:46.580745Z", "shell.execute_reply": "2022-07-27T19:15:46.579405Z" } }, "outputs": [ { "data": { "text/plain": [ "['file.0.txt', 'file.1.txt', 'file.2.txt']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filenames = [\"file.{}.txt\".format(i) for i in range(10)]\n", "\n", "filenames[:3]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:15:46.585130Z", "iopub.status.busy": "2022-07-27T19:15:46.584620Z", "iopub.status.idle": "2022-07-27T19:15:46.590514Z", "shell.execute_reply": "2022-07-27T19:15:46.589931Z" } }, "outputs": [], "source": [ "import random, time\n", "\n", "\n", "def parse_file(fn: str) -> list:\n", " \"\"\" Returns a list work items of unknown length \"\"\"\n", " time.sleep(random.random())\n", " return [random.random() for _ in range(random.randint(1, 10))]\n", "\n", "def process_item(x: float):\n", " \"\"\" Process each work item \"\"\"\n", " time.sleep(random.random() / 4)\n", " return x + 1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:15:46.593382Z", "iopub.status.busy": "2022-07-27T19:15:46.592886Z", "iopub.status.idle": "2022-07-27T19:16:00.029676Z", "shell.execute_reply": "2022-07-27T19:16:00.028597Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 8.63 ms, sys: 1 ms, total: 9.63 ms\n", "Wall time: 13.4 s\n" ] } ], "source": [ "%%time\n", "\n", "# This takes around 10-20s\n", "\n", "results = []\n", "\n", "for fn in filenames:\n", " L = parse_file(fn)\n", " for x in L:\n", " out = process_item(x)\n", " results.append(out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start Dask Client\n", "-----------------\n", "\n", "We'll need a Dask client in order to manage dynamic workloads" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:16:00.033909Z", "iopub.status.busy": "2022-07-27T19:16:00.033701Z", "iopub.status.idle": "2022-07-27T19:16:01.185004Z", "shell.execute_reply": "2022-07-27T19:16:01.184477Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "

Client

\n", "

Client-8cd18990-0de0-11ed-9f5a-000d3a8f7959

\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "
Connection method: Cluster objectCluster type: distributed.LocalCluster
\n", " Dashboard: http://10.1.1.64:8787/status\n", "
\n", "\n", " \n", "
\n", "

Cluster Info

\n", "
\n", "
\n", "
\n", "
\n", "

LocalCluster

\n", "

136395a1

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", "
\n", " Dashboard: http://10.1.1.64:8787/status\n", " \n", " Workers: 1\n", "
\n", " Total threads: 6\n", " \n", " Total memory: 6.78 GiB\n", "
Status: runningUsing processes: False
\n", "\n", "
\n", " \n", "

Scheduler Info

\n", "
\n", "\n", "
\n", "
\n", "
\n", "
\n", "

Scheduler

\n", "

Scheduler-32e093ca-1ad3-4cda-a0bf-b68890befde7

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " Comm: inproc://10.1.1.64/8026/1\n", " \n", " Workers: 1\n", "
\n", " Dashboard: http://10.1.1.64:8787/status\n", " \n", " Total threads: 6\n", "
\n", " Started: Just now\n", " \n", " Total memory: 6.78 GiB\n", "
\n", "
\n", "
\n", "\n", "
\n", " \n", "

Workers

\n", "
\n", "\n", " \n", "
\n", "
\n", "
\n", "
\n", " \n", "

Worker: 0

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", " \n", "\n", "
\n", " Comm: inproc://10.1.1.64/8026/4\n", " \n", " Total threads: 6\n", "
\n", " Dashboard: http://10.1.1.64:39259/status\n", " \n", " Memory: 6.78 GiB\n", "
\n", " Nanny: None\n", "
\n", " Local directory: /home/runner/work/dask-examples/dask-examples/applications/dask-worker-space/worker-v95r5hsj\n", "
\n", "
\n", "
\n", "
\n", " \n", "\n", "
\n", "
\n", "\n", "
\n", "
\n", "
\n", "
\n", " \n", "\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dask.distributed import Client\n", "\n", "client = Client(processes=False, n_workers=1, threads_per_worker=6)\n", "client" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1: Use as_completed\n", "-------------------\n", "\n", "The [as_completed](https://docs.dask.org/en/latest/futures.html#distributed.as_completed) iterator lets us handle futures as they complete. We can then submit more data on the fly.\n", "\n", "- We submit a task for each of our filenames\n", "- We also compute the length of each of the returned lists\n", "- As those lengths return, we submit off a new task to get each item of that list. We do this at higher priority, so that we process existing data before we collect new data.\n", "- We wait on all of the returned results" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:16:01.188841Z", "iopub.status.busy": "2022-07-27T19:16:01.188332Z", "iopub.status.idle": "2022-07-27T19:16:03.802445Z", "shell.execute_reply": "2022-07-27T19:16:03.801713Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 764 ms, sys: 92.2 ms, total: 856 ms\n", "Wall time: 2.6 s\n" ] }, { "data": { "text/plain": [ "[1.855253279843388,\n", " 1.7726329902181985,\n", " 1.259836642564709,\n", " 1.3256571039929872,\n", " 1.5880998029929763,\n", " 1.5138789437567746,\n", " 1.5792286074896462,\n", " 1.3147689951092234,\n", " 1.3172076038757292,\n", " 1.262171473735921,\n", " 1.4714750086893678,\n", " 1.201396011598346,\n", " 1.0228797089919106,\n", " 1.100749927496291,\n", " 1.312524856134222,\n", " 1.6853261123541268,\n", " 1.7828341588461032,\n", " 1.353256049336657,\n", " 1.5560048785929426,\n", " 1.909180985889757,\n", " 1.3652870032952238,\n", " 1.0200787324818752,\n", " 1.6887029263541482,\n", " 1.8419102255817514,\n", " 1.7088922634312127,\n", " 1.0272413357700962,\n", " 1.9966520355684192,\n", " 1.7515633492540368,\n", " 1.1784571947389346,\n", " 1.2626529965584385,\n", " 1.776210436365392,\n", " 1.1980741841717615,\n", " 1.6535182981420282,\n", " 1.652425843466223,\n", " 1.8679564994356905,\n", " 1.4829294673653362,\n", " 1.5798547500541158,\n", " 1.140733801463906,\n", " 1.8522280296306772]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "\n", "from dask.distributed import as_completed\n", "import operator\n", "\n", "lists = client.map(parse_file, filenames, pure=False)\n", "lengths = client.map(len, lists)\n", "\n", "mapping = dict(zip(lengths, lists))\n", "\n", "futures = []\n", "\n", "for future in as_completed(lengths):\n", " n = future.result()\n", " L = mapping[future]\n", " for i in range(n):\n", " new = client.submit(operator.getitem, L, i, priority=1)\n", " new = client.submit(process_item, new, priority=1)\n", " futures.append(new)\n", " \n", "client.gather(futures)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2: Use async/await to handle single file processing locally\n", "-----------------------------------------------------------\n", "\n", "We can also handle the concurrency here within our local process. This requires you to understand async/await syntax, but is generally powerful and arguably simpler than the `as_completed` approach above." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:16:03.805244Z", "iopub.status.busy": "2022-07-27T19:16:03.805030Z", "iopub.status.idle": "2022-07-27T19:16:03.812380Z", "shell.execute_reply": "2022-07-27T19:16:03.811568Z" } }, "outputs": [], "source": [ "import asyncio\n", "\n", "async def f(fn):\n", " \"\"\" Handle the lifecycle of a single file \"\"\"\n", " future = client.submit(parse_file, fn, pure=False)\n", " length_future = client.submit(len, future)\n", " length = await length_future\n", " \n", " futures = [client.submit(operator.getitem, future, i, priority=10) \n", " for i in range(length)]\n", " futures = client.map(process_item, futures, priority=10)\n", " return futures\n", "\n", "async def run_all(filenames):\n", " list_of_list_of_futures = await asyncio.gather(*[f(fn) for fn in filenames])\n", " futures = sum(list_of_list_of_futures, [])\n", " return await client.gather(futures)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now need to run this function in the same event loop as our client is running. If we had started our client asynchronously, then we could have done this:\n", "\n", "```python\n", "client = await Client(asynchronous=True)\n", "\n", "await run_all(filenames)\n", "```\n", "\n", "However, because we started our client without the `asynchronous=True` flag the event loop is actually running in a separate thread, so we'll have to ask the client to run this for us." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:16:03.815770Z", "iopub.status.busy": "2022-07-27T19:16:03.815139Z", "iopub.status.idle": "2022-07-27T19:16:06.022696Z", "shell.execute_reply": "2022-07-27T19:16:06.021940Z" } }, "outputs": [ { "data": { "text/plain": [ "[1.800289116905064,\n", " 1.9871081844250762,\n", " 1.4822374404826442,\n", " 1.5398817452739677,\n", " 1.7815421767702233,\n", " 1.799086158704496,\n", " 1.3684979870560656,\n", " 1.7599086452147468,\n", " 1.481710657277671,\n", " 1.498441345100709,\n", " 1.4189325403582265,\n", " 1.4919506969852225,\n", " 1.784110902042696,\n", " 1.300637697987958,\n", " 1.67384020477976,\n", " 1.9299992134781654,\n", " 1.5169156883991846,\n", " 1.3547854189286748,\n", " 1.0158519081255195,\n", " 1.2905126990047529,\n", " 1.5902047470140799,\n", " 1.3370247692084685,\n", " 1.8606232787906918,\n", " 1.0593086235586204,\n", " 1.2708399290512835,\n", " 1.6014307072413518,\n", " 1.719953843514872,\n", " 1.811378106188461,\n", " 1.196149275085125,\n", " 1.1182782176944635,\n", " 1.233133146375533,\n", " 1.9828811027418132,\n", " 1.0789456550316718,\n", " 1.8491111490176726,\n", " 1.895451013583132,\n", " 1.3219157394238925,\n", " 1.7080716916510053,\n", " 1.3280857033684856,\n", " 1.0402444828283657,\n", " 1.6131248314712203,\n", " 1.2100254915413697,\n", " 1.092415131966796,\n", " 1.93960521763009,\n", " 1.793696097887303,\n", " 1.6268308430745564,\n", " 1.1389083858458733,\n", " 1.6461270076543006,\n", " 1.087262577270262,\n", " 1.5348789887982262,\n", " 1.7953259319984842]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "client.sync(run_all, filenames)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3: Submit tasks from tasks\n", "--------------------------\n", "\n", "We can also submit tasks that themselves submit more tasks. See [documentation here](https://docs.dask.org/en/latest/futures.html#submit-tasks-from-tasks)." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2022-07-27T19:16:06.025931Z", "iopub.status.busy": "2022-07-27T19:16:06.025342Z", "iopub.status.idle": "2022-07-27T19:16:08.174419Z", "shell.execute_reply": "2022-07-27T19:16:08.173647Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 357 ms, sys: 20.7 ms, total: 378 ms\n", "Wall time: 2.14 s\n" ] } ], "source": [ "%%time\n", "\n", "from dask.distributed import get_client, secede, rejoin\n", "\n", "def f(fn):\n", " L = parse_file(fn)\n", " client = get_client()\n", " \n", " futures = client.map(process_item, L, priority=10)\n", " secede()\n", " results = client.gather(futures)\n", " rejoin()\n", " return results\n", "\n", "futures = client.map(f, filenames, pure=False)\n", "results = client.gather(futures)" ] } ], "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 }