File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11"""Demonstrate Parts 1 and 2 of Hackersandslackers Asyncio tutorial series"""
22import asyncio
3+ import time
34
45from 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 ))
Original file line number Diff line number Diff line change 1- """Script initialization ."""
1+ """Make hundreds of requests concurrently and save responses to disk ."""
22import asyncio
3+ import time
34
45from aiohttp import ClientSession
56
1213from .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+ )
Original file line number Diff line number Diff line change 11"""Create and execute asynchronous tasks in a loop."""
22import asyncio
3+ import time
34
45from asyncio_tutorial .logger import LOGGER
56
910from .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+ )
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 11"""Create task from coroutine which displays a value after a delay."""
2- from typing import Callable
32import asyncio
43from asyncio import Task
4+ from typing import Callable
55
66
77async def create_task (coroutine : Callable ) -> Task :
Original file line number Diff line number Diff line change 1616 "connection" : "keep-alive" ,
1717 "accept" : "*/*" ,
1818}
19-
Original file line number Diff line number Diff line change @@ -16,4 +16,4 @@ pycares==4.0.0
1616pycparser == 2.20 ; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.4.0"
1717typing-extensions == 3.10.0.2 ; python_version >= "3.6"
1818win32-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"
You can’t perform that action at this time.
0 commit comments