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