diff --git a/src/examples/performance_tips.ipynb b/src/examples/performance_tips.ipynb index a048b8ea6..69227c04e 100644 --- a/src/examples/performance_tips.ipynb +++ b/src/examples/performance_tips.ipynb @@ -49,19 +49,21 @@ "import time\n", "from typing import Any\n", "\n", - "\n", "class DummyTask(Task):\n", " def do_run(self, input: Any, task_span: TaskSpan) -> Any:\n", " time.sleep(2)\n", " print(\"Task1 complete\")\n", " return input\n", + "\n", "tracer = NoOpTracer()\n", "\n", "task_input = [\"A\", \"B\", \"C\", \"D\"]\n", "task = DummyTask()\n", "\n", "\n", - "result = task.run_concurrently(task_input, tracer) # this finishes in 2 seconds instead of 8\n", + "result = task.run_concurrently(\n", + " task_input, tracer\n", + ") # this finishes in 2 seconds instead of 8\n", "result" ] }, @@ -88,13 +90,14 @@ " time.sleep(2)\n", " print(\"Task2 complete\")\n", " return input\n", - " \n", + "\n", + "\n", "# initialize all tasks and inputs\n", "task_1 = DummyTask()\n", "task_2 = DummyTask2()\n", "\n", "task_input_1 = list(range(10))\n", - "task_input_2 = list(range(10,20))" + "task_input_2 = list(range(10, 20))" ] }, { @@ -146,9 +149,11 @@ "jobs = list(zip(repeat(task_1), task_input_1)) + list(zip(repeat(task_2), task_input_2))\n", "\n", "with ThreadPoolExecutor(max_workers=20) as executor:\n", - " result = list(executor.map(lambda task_input: task_input[0].run(task_input[1], tracer), jobs))\n", - " print(\"Task 1 result:\", result[:len(task_input_1)])\n", - " print(\"Task 2 result:\", result[len(task_input_1):]) " + " result = list(\n", + " executor.map(lambda job: job[0].run(job[1], tracer), jobs)\n", + " )\n", + " print(\"Task 1 result:\", result[: len(task_input_1)])\n", + " print(\"Task 2 result:\", result[len(task_input_1) :])" ] }, { @@ -167,9 +172,14 @@ "outputs": [], "source": [ "with ThreadPoolExecutor(max_workers=2) as executor:\n", - " results = list(executor.map(lambda task_input: task_input[0].run_concurrently(task_input[1], tracer), [(task_1, task_input_1), (task_2, task_input_2)]))\n", - " print(\"Task 1 result:\", result[:len(task_input_1)])\n", - " print(\"Task 2 result:\", result[len(task_input_1):])" + " results = list(\n", + " executor.map(\n", + " lambda job: job[0].run_concurrently(job[1], tracer),\n", + " [(task_1, task_input_1), (task_2, task_input_2)],\n", + " )\n", + " )\n", + " print(\"Task 1 result:\", result[: len(task_input_1)])\n", + " print(\"Task 2 result:\", result[len(task_input_1) :])" ] }, { @@ -183,12 +193,6 @@ "\n", "If tasks are CPU bound, the abovementioned code will not help. In that case, replace the `ThreadPoolExecutor` with a `ProcessPoolExecutor`." ] - }, - { - "cell_type": "markdown", - "id": "f946ac08", - "metadata": {}, - "source": [] } ], "metadata": {