Spaces:
No application file
No application file
File size: 3,601 Bytes
28e8c09 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | {
"cells": [
{
"cell_type": "markdown",
"id": "5c291475-8c7c-461c-9b12-545a887b2432",
"metadata": {},
"source": [
"# Async Python\n",
"\n",
"## A briefing on asynchronous python coding, essential in Agent engineering"
]
},
{
"cell_type": "markdown",
"id": "538fa044",
"metadata": {},
"source": [
"Here is a masterful tutorial by you-know-who with exercises and comparisons.\n",
"\n",
"https://chatgpt.com/share/680648b1-b0a0-8012-8449-4f90b540886c\n",
"\n",
"This includes how to run async code from a python module.\n",
"\n",
"### And now some examples:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "09f5662a",
"metadata": {},
"outputs": [],
"source": [
"# Let's define an async function\n",
"\n",
"import asyncio\n",
"\n",
"async def do_some_work():\n",
" print(\"Starting work\")\n",
" await asyncio.sleep(1)\n",
" print(\"Work complete\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07ab3abf",
"metadata": {},
"outputs": [],
"source": [
"# What will this do?\n",
"\n",
"do_some_work()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d681b6d",
"metadata": {},
"outputs": [],
"source": [
"# OK let's try that again!\n",
"\n",
"await do_some_work()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea867090",
"metadata": {},
"outputs": [],
"source": [
"# What's wrong with this?\n",
"\n",
"async def do_a_lot_of_work():\n",
" do_some_work()\n",
" do_some_work()\n",
" do_some_work()\n",
"\n",
"await do_a_lot_of_work()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9c75c3f",
"metadata": {},
"outputs": [],
"source": [
"# Interesting warning! Let's fix it\n",
"\n",
"async def do_a_lot_of_work():\n",
" await do_some_work()\n",
" await do_some_work()\n",
" await do_some_work()\n",
"\n",
"await do_a_lot_of_work()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "720cf3f5",
"metadata": {},
"outputs": [],
"source": [
"# And now let's do it in parallel\n",
"# It's important to recognize that this is not \"multi-threading\" in the way that you may be used to\n",
"# The asyncio library is running on a single thread, but it's using a loop to switch between tasks while one is waiting\n",
"\n",
"async def do_a_lot_of_work_in_parallel():\n",
" await asyncio.gather(do_some_work(), do_some_work(), do_some_work())\n",
"\n",
"await do_a_lot_of_work_in_parallel()"
]
},
{
"cell_type": "markdown",
"id": "230f85de",
"metadata": {},
"source": [
"### Finally - try writing a python module that calls do_a_lot_of_work_in_parallel\n",
"\n",
"See the link at the top; you'll need something like this in your module:\n",
"\n",
"```python\n",
"if __name__ == \"__main__\":\n",
" asyncio.run(do_a_lot_of_work_in_parallel())\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|