Skip to content

Commit f00007d

Browse files
committed
Log execution time in each future result.
1 parent 436b96f commit f00007d

8 files changed

Lines changed: 147 additions & 97 deletions

File tree

asyncio_tutorial/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Demonstrate Parts 1 and 2 of Hackersandslackers Asyncio tutorial series"""
22
import asyncio
3+
import time
34

45
from asyncio_tutorial.logger import LOGGER
56

@@ -14,5 +15,6 @@ def init_script():
1415
1. Intro to Async Python: https://hackersandslackers.com/python-concurrency-asyncio/
1516
2. Intro to Async Python: https://hackersandslackers.com/async-requests-with-aiohttp/
1617
"""
17-
asyncio.run(asyncio_intro_tutorial())
18-
asyncio.run(aiohttp_aiofiles_tutorial())
18+
start_time = time.perf_counter()
19+
asyncio.run(asyncio_intro_tutorial(start_time))
20+
asyncio.run(aiohttp_aiofiles_tutorial(start_time))

asyncio_tutorial/part_II_aiohttp_aiofiles/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Script initialization."""
1+
"""Make hundreds of requests concurrently and save responses to disk."""
22
import asyncio
3+
import time
34

45
from aiohttp import ClientSession
56

@@ -12,12 +13,18 @@
1213
from .tasks import create_tasks
1314

1415

15-
async def aiohttp_aiofiles_tutorial():
16-
"""Open async HTTP session & execute created tasks."""
16+
async def aiohttp_aiofiles_tutorial(start_time):
17+
"""
18+
Open async HTTP session & execute created tasks.
19+
20+
:param float start_time: Counter representing the time the script was initialized.
21+
"""
1722
LOGGER.info(f"Asyncio tutorial Part II: HTTP Requests with Aiohttp & Aiofiles.")
1823
future = register_future()
1924
async with ClientSession(headers=HTML_HEADERS) as session:
2025
tasks = await create_tasks(session, urls, EXPORT_DIR)
2126
inspect_event_loop()
2227
await asyncio.gather(*tasks)
23-
future.set_result(f"Saved {len(urls)} files to `{EXPORT_DIR}`")
28+
future.set_result(
29+
f"Executed {__name__} in {time.perf_counter() - start_time:0.2f} seconds."
30+
)
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Create and execute asynchronous tasks in a loop."""
22
import asyncio
3+
import time
34

45
from asyncio_tutorial.logger import LOGGER
56

@@ -9,8 +10,12 @@
910
from .tasks import create_task
1011

1112

12-
async def asyncio_intro_tutorial():
13-
"""Demo of an asynchronous script's lifecycle."""
13+
async def asyncio_intro_tutorial(start_time: float):
14+
"""
15+
Demo of an asynchronous script's lifecycle.
16+
17+
:param float start_time: Counter representing the time the script was initialized.
18+
"""
1419
LOGGER.info(f"Asyncio tutorial Part I: Intro to Asyncio.")
1520
task_list = []
1621
future = register_future()
@@ -19,4 +24,6 @@ async def asyncio_intro_tutorial():
1924
task_list.append(task)
2025
inspect_event_loop()
2126
await asyncio.gather(*task_list)
22-
future.set_result("Done")
27+
future.set_result(
28+
f"Executed {__name__} in {time.perf_counter() - start_time:0.2f} seconds."
29+
)

asyncio_tutorial/part_I_asyncio_intro/futures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def loop_completed(result: str):
2121
"""
2222
Callback function fired when loop is complete.
2323
24-
:param str result: String describing the state of the loop
24+
:param str result: Message describing the state of the loop & its execution time.
2525
"""
2626
LOGGER.success(
27-
f"Loop completed with result: {result} \
27+
f"{result} \
2828
\n--------------------------------------"
2929
)

asyncio_tutorial/part_I_asyncio_intro/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Create task from coroutine which displays a value after a delay."""
2-
from typing import Callable
32
import asyncio
43
from asyncio import Task
4+
from typing import Callable
55

66

77
async def create_task(coroutine: Callable) -> Task:

config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@
1616
"connection": "keep-alive",
1717
"accept": "*/*",
1818
}
19-

poetry.lock

Lines changed: 118 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ pycares==4.0.0
1616
pycparser==2.20; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.4.0"
1717
typing-extensions==3.10.0.2; python_version >= "3.6"
1818
win32-setctime==1.0.3; sys_platform == "win32" and python_version >= "3.5"
19-
yarl==1.6.3; python_version >= "3.6"
19+
yarl==1.7.0; python_version >= "3.6"

0 commit comments

Comments
 (0)