Skip to content

Commit

Permalink
refactor: renaming and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasKoehneckeAA committed Feb 20, 2024
1 parent 4f479c2 commit 001b194
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/examples/performance_tips.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand All @@ -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))"
]
},
{
Expand Down Expand Up @@ -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) :])"
]
},
{
Expand All @@ -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) :])"
]
},
{
Expand All @@ -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": {
Expand Down

0 comments on commit 001b194

Please sign in to comment.