Skip to content

Commit c50848b

Browse files
authored
feat: Add python async io example (#161)
1 parent d120c9f commit c50848b

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

with-python-asyncio/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
venv

with-python-asyncio/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Plus Five Five, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

with-python-asyncio/app.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
import time
3+
import resend
4+
5+
def send_email(n):
6+
start_time = time.time()
7+
8+
params: resend.Emails.SendParams = {
9+
"from": "[email protected]",
10+
"to": ["[email protected]"],
11+
"subject": "hi",
12+
"html": "<strong>hello, world!</strong>",
13+
}
14+
15+
email: resend.Email = resend.Emails.send(params)
16+
print("Email ID: #{} processed in #{}".format(email["id"], time.time() - start_time))
17+
18+
async def send_in_parallel():
19+
start_time = time.time()
20+
21+
await asyncio.gather(
22+
asyncio.to_thread(send_email, 1),
23+
asyncio.to_thread(send_email, 2),
24+
asyncio.to_thread(send_email, 3),
25+
asyncio.to_thread(send_email, 4))
26+
27+
print(f"Total time {time.time() - start_time}")
28+
29+
if __name__ == "__main__":
30+
loop = asyncio.new_event_loop()
31+
asyncio.set_event_loop(loop)
32+
task = send_in_parallel()
33+
loop.run_until_complete(task)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
asyncio
2+
resend

0 commit comments

Comments
 (0)